diff options
author | Mavridis Philippe <mavridisf@gmail.com> | 2024-08-07 19:13:02 +0300 |
---|---|---|
committer | Mavridis Philippe <mavridisf@gmail.com> | 2024-08-07 19:23:24 +0300 |
commit | 04b5a62b8d9f5ff8240f25361046f2a5d58e8262 (patch) | |
tree | 98b126454cdf68d544e138d7e8b31d5fd45b72c2 /kue/vector.cpp | |
parent | 83ba00b7e569587d50383ff06a70148042ca780e (diff) | |
download | tdegames-04b5a62b8d9f5ff8240f25361046f2a5d58e8262.tar.gz tdegames-04b5a62b8d9f5ff8240f25361046f2a5d58e8262.zip |
Add Kue billiards gamefeat/kue
Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
Diffstat (limited to 'kue/vector.cpp')
-rw-r--r-- | kue/vector.cpp | 85 |
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)); +} |