From adafae2d8418783018052b94c16f4a05a88c7b33 Mon Sep 17 00:00:00 2001 From: Amy Retzerau Date: Wed, 12 Nov 2025 10:40:32 +0100 Subject: [PATCH] refactor: made everything mutable --- fastmath.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/fastmath.hpp b/fastmath.hpp index b90a147..751e042 100644 --- a/fastmath.hpp +++ b/fastmath.hpp @@ -3,7 +3,6 @@ #include #include -#include #include #include #include @@ -20,7 +19,7 @@ struct decimal { - const int32_t i; + int32_t i; decimal() : i(0) {} decimal(float i) : i(TO_INT(i)) {} @@ -60,6 +59,9 @@ struct decimal { friend bool operator==(const decimal &d1, const decimal &d2) { return d1.i == d2.i; } + friend bool operator!=(const decimal &d1, const decimal &d2) { + return d1.i != d2.i; + } decimal &operator=(decimal const &in) { if (this != &in) { @@ -153,6 +155,7 @@ template struct vec { } return res; } + decimal &operator[](const int &i) { return v[i]; } decimal len_sq() { return *this * *this; } @@ -166,7 +169,21 @@ template struct vec { protected: decimal v[n]; }; +struct vec2 : public vec<2, vec2> { + vec2() : vec<2, vec2>() {} + + vec2(float x, float y) : vec<2, vec2>({decimal(x), decimal(y)}) {} + + vec2(double x, double y) : vec<2, vec2>({decimal(x), decimal(y)}) {} + + vec2(int32_t x, int32_t y) : vec<2, vec2>({decimal(x), decimal(y)}) {} + + vec2(decimal x, decimal y) : vec<2, vec2>({x, y}) {} + + decimal &x() { return v[0]; } + decimal &y() { return v[1]; } +}; struct vec3 : public vec<3, vec3> { vec3() : vec<3, vec3>() {} @@ -182,14 +199,34 @@ struct vec3 : public vec<3, vec3> { vec3(decimal x, decimal y, decimal z) : vec<3, vec3>({x, y, z}) {} - decimal x() const { return v[0]; } - decimal y() const { return v[1]; } - decimal z() const { return v[2]; } + decimal &x() { return v[0]; } + decimal &y() { return v[1]; } + decimal &z() { return v[2]; } - vec3 cross(const vec3 &v) { + vec3 cross(vec3 &v) { return vec3((y() * v.z()) - (z() * v.y()), (z() * v.x()) - (x() * v.z()), (x() * v.y()) - (y() * v.x())); } }; +struct vec4 : public vec<4, vec4> { + + vec4() : vec<4, vec4>() {} + + vec4(float x, float y, float z, float w) + : vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {} + + vec4(double x, double y, double z, double w) + : vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {} + + vec4(int32_t x, int32_t y, int32_t z, int32_t w) + : vec<4, vec4>({decimal(x), decimal(y), decimal(z), decimal(w)}) {} + + vec4(decimal x, decimal y, decimal z) : vec<4, vec4>({x, y, z}) {} + + decimal &x() { return v[0]; } + decimal &y() { return v[1]; } + decimal &z() { return v[2]; } + decimal &w() { return v[3]; } +}; #endif