refactor: made everything mutable

This commit is contained in:
2025-11-12 10:40:32 +01:00
parent 949578f0db
commit adafae2d84

View File

@@ -3,7 +3,6 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <sstream>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
@@ -20,7 +19,7 @@
struct decimal { struct decimal {
const int32_t i; int32_t i;
decimal() : i(0) {} decimal() : i(0) {}
decimal(float i) : i(TO_INT(i)) {} decimal(float i) : i(TO_INT(i)) {}
@@ -60,6 +59,9 @@ struct decimal {
friend bool operator==(const decimal &d1, const decimal &d2) { friend bool operator==(const decimal &d1, const decimal &d2) {
return d1.i == d2.i; return d1.i == d2.i;
} }
friend bool operator!=(const decimal &d1, const decimal &d2) {
return d1.i != d2.i;
}
decimal &operator=(decimal const &in) { decimal &operator=(decimal const &in) {
if (this != &in) { if (this != &in) {
@@ -153,6 +155,7 @@ template <int n, class Dev> struct vec {
} }
return res; return res;
} }
decimal &operator[](const int &i) { return v[i]; }
decimal len_sq() { return *this * *this; } decimal len_sq() { return *this * *this; }
@@ -166,7 +169,21 @@ template <int n, class Dev> struct vec {
protected: protected:
decimal v[n]; 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> { struct vec3 : public vec<3, vec3> {
vec3() : 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}) {} vec3(decimal x, decimal y, decimal z) : vec<3, vec3>({x, y, z}) {}
decimal x() const { return v[0]; } decimal &x() { return v[0]; }
decimal y() const { return v[1]; } decimal &y() { return v[1]; }
decimal z() const { return v[2]; } decimal &z() { return v[2]; }
vec3 cross(const vec3 &v) { vec3 cross(vec3 &v) {
return vec3((y() * v.z()) - (z() * v.y()), return vec3((y() * v.z()) - (z() * v.y()),
(z() * v.x()) - (x() * v.z()), (z() * v.x()) - (x() * v.z()),
(x() * v.y()) - (y() * v.x())); (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 #endif