#include "vector.h" // Creates a vector with between two points vector::vector(const point &source, const point &dest) { _magnitude = source.distance(dest); _direction = source.angle(dest); } // Creates an empty vector vector::vector() { _magnitude = 0.0; _direction = 0.0; } // Copy another vector object vector::vector(const vector& v) { _magnitude = v._magnitude; _direction = v._direction; } // Set the X component void vector::setComponentX(double x) { setComponents(x, componentY()); } // Set the Y component void vector::setComponentY(double y) { setComponents(componentX(), y); } // Operations with another vector performs vector math vector vector::operator+(const vector& v) { double x = componentX() + v.componentX(); double y = componentY() + v.componentY(); return vector(sqrt((x * x) + (y * y)), atan2(y, x)); } vector vector::operator-(const vector& v) { double x = componentX() - v.componentX(); double y = componentY() - v.componentY(); return vector(sqrt((x * x) + (y * y)), atan2(y, x)); } vector& vector::operator+=(const vector& v) { setComponents(componentX() + v.componentX(), componentY() + v.componentY()); return *this; } vector& vector::operator-=(const vector& v) { setComponents(componentX() - v.componentX(), componentY() - v.componentY()); return *this; } double vector::operator*(const vector& v) { return ((componentX() * v.componentX()) + (componentY() * v.componentY())); } // Operations with a single double value affects the magnitude vector& vector::operator+= (double m) { _magnitude += m; return *this; } vector& vector::operator-= (double m) { _magnitude -= m; return *this; } vector& vector::operator*= (double m) { _magnitude *= m; return *this; } vector& vector::operator/= (double m) { _magnitude /= m; return *this; } // Sets both components at once (the only way to do it efficently) void vector::setComponents(double x, double y) { _direction = atan2(y, x); _magnitude = sqrt((x * x) + (y * y)); }