summaryrefslogtreecommitdiff
path: root/src/math/Vector2D.h
diff options
context:
space:
mode:
authoraap <aap@papnet.eu>2019-05-15 16:52:37 +0200
committeraap <aap@papnet.eu>2019-05-15 16:52:37 +0200
commit600bf0351476a5a21aabb5429132ddf7f52ac0b9 (patch)
treed8e48b3a581679e33830fb7c98ed69e1e242e2c2 /src/math/Vector2D.h
first commit
Diffstat (limited to 'src/math/Vector2D.h')
-rw-r--r--src/math/Vector2D.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/math/Vector2D.h b/src/math/Vector2D.h
new file mode 100644
index 00000000..3c0013d4
--- /dev/null
+++ b/src/math/Vector2D.h
@@ -0,0 +1,37 @@
+#pragma once
+
+class CVector2D
+{
+public:
+ float x, y;
+ CVector2D(void) {}
+ CVector2D(float x, float y) : x(x), y(y) {}
+ CVector2D(const CVector &v) : x(v.x), y(v.y) {}
+ float Magnitude(void) const { return sqrt(x*x + y*y); }
+ float MagnitudeSqr(void) const { return x*x + y*y; }
+
+ void Normalise(void){
+ float sq = MagnitudeSqr();
+ if(sq > 0.0f){
+ float invsqrt = 1.0f/sqrt(sq);
+ x *= invsqrt;
+ y *= invsqrt;
+ }else
+ x = 0.0f;
+ }
+ CVector2D operator-(const CVector2D &rhs) const {
+ return CVector2D(x-rhs.x, y-rhs.y);
+ }
+ CVector2D operator+(const CVector2D &rhs) const {
+ return CVector2D(x+rhs.x, y+rhs.y);
+ }
+ CVector2D operator*(float t) const {
+ return CVector2D(x*t, y*t);
+ }
+};
+
+inline float
+CrossProduct2D(const CVector2D &v1, const CVector2D &v2)
+{
+ return v1.x*v2.y - v1.y*v2.x;
+}