diff options
Diffstat (limited to 'frontend/shared_cpp/mathlib.h')
-rw-r--r-- | frontend/shared_cpp/mathlib.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/frontend/shared_cpp/mathlib.h b/frontend/shared_cpp/mathlib.h index 7595045..2784f6a 100644 --- a/frontend/shared_cpp/mathlib.h +++ b/frontend/shared_cpp/mathlib.h @@ -2,6 +2,11 @@ #include <cstdio> #include <cstdlib> #include <cstring> +#include <cmath> + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#define ABS(x) (x < 0 ? -x : x) struct Vector2 { float x = 0; @@ -11,6 +16,12 @@ struct Vector2 { return { x + other.x, y + other.y }; } + Vector2& operator+=(Vector2 other) { + x += other.x; + y += other.y; + return *this; + } + Vector2 operator-(Vector2 other) { return { x - other.x, y - other.y }; } @@ -19,6 +30,10 @@ struct Vector2 { return { x * s, y * s }; } + Vector2 operator/(float s) { + return { x / s, y / s }; + } + float dot(Vector2 other) { return x * other.x + y * other.y; } @@ -45,6 +60,20 @@ struct Vector2 { void printDebug(const char* name) { printf("%s=Vector2(%f, %f)\n", name, x, y); } + + float determinant(Vector2 other) { + // + // [ a b ] + // [ c d ] + // + // [ x other.x ] + // [ y other.y ] + // + // det = a * d - b * c + // det = x * other.y - other.x * y + // + return x * other.y - other.x * y; + } }; struct Vector3 { |