2DVector.cpp
Zdrojový soubor pro třídu Vector
c-plus-plus
#include "2DVector.h"
#include <math.h>
#include <exception>
void Vector::Compute()
{
size = sqrt(x * x + y * y);
if(size == 0)
{
angle = -1;
return;
}
angle = asin(y / size) * 57.2957795;
if(angle < 0)
angle += 360;
}
Vector::Vector(double x, double y)
{
Vector::x = x;
Vector::y = y;
Compute();
}
Vector::Vector(const Point & A, const Point & B)
{
x = B.x - A.x;
y = B.y - A.y;
Compute();
}
Vector::Vector(double ax, double ay, double bx, double by)
{
x = bx - ax;
y = by - ay;
Compute();
}
const Vector & Vector::operator+=(const Vector & vector)
{
x += vector.x;
y += vector.y;
Compute();
return *this;
}
const Vector & Vector::operator-=(const Vector & vector)
{
x -= vector.x;
y -= vector.y;
Compute();
return *this;
}
const Vector & Vector::operator*=(double num)
{
if(num == 0)
angle = -1;
x *= num;
y *= num;
size*= num;
return *this;
}
const Vector & Vector::operator/=(double num) throw(DivisionByZero)
{
if(num == 0)
throw DivisionByZero();
x /= num;
y /= num;
size /= num;
return *this;
}
double Vector::operator%(const Vector & vector) const
{
if(angle == -1 || vector.angle == -1)
return -1;
double ang = angle - vector.angle;
if(ang <= 0)
ang += 360;
if(ang > 180)
ang = 360 - ang;
return ang;
}
void Vector::Set(double x, double y)
{
Vector::x = x;
Vector::y = y;
Compute();
}
void Vector::Set(const Point & A, const Point & B)
{
x = B.x - A.x;
y = B.y - A.y;
Compute();
}
void Vector::Set(double ax, double ay, double bx, double by)
{
x = bx - ax;
y = by - ay;
Compute();
}
Neformátovaný
Přidáno: 18.6.2013
Expirace: Neuvedeno