summaryrefslogtreecommitdiffstats
path: root/kue/vector.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kue/vector.cpp')
-rw-r--r--kue/vector.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/kue/vector.cpp b/kue/vector.cpp
new file mode 100644
index 00000000..40aed057
--- /dev/null
+++ b/kue/vector.cpp
@@ -0,0 +1,85 @@
+#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));
+}