Skip to content

Commit e298a46

Browse files
feat(sourcepp): add more math operators to vector structs
1 parent 051dc1c commit e298a46

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

include/sourcepp/math/Vector.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <cmath>
4+
35
#include "Integer.h"
46

57
namespace sourcepp::math {
@@ -13,14 +15,62 @@ struct Vec2 {
1315
return {this->x + other.x, this->y + other.y};
1416
}
1517

18+
void operator+=(const Vec2& other) {
19+
this->x += other.x;
20+
this->y += other.y;
21+
}
22+
1623
[[nodiscard]] Vec2 operator-(const Vec2& other) const {
1724
return {this->x - other.x, this->y - other.y};
1825
}
1926

27+
void operator-=(const Vec2& other) {
28+
this->x -= other.x;
29+
this->y -= other.y;
30+
}
31+
32+
[[nodiscard]] Vec2 operator*(Arithmetic auto scalar) const {
33+
return {this->x * scalar, this->y * scalar};
34+
}
35+
36+
void operator*=(Arithmetic auto scalar) {
37+
this->x *= scalar;
38+
this->y *= scalar;
39+
}
40+
41+
[[nodiscard]] Vec2 operator/(Arithmetic auto scalar) const {
42+
return {this->x / scalar, this->y / scalar};
43+
}
44+
45+
void operator/=(Arithmetic auto scalar) {
46+
this->x /= scalar;
47+
this->y /= scalar;
48+
}
49+
2050
[[nodiscard]] bool operator==(const Vec2& other) const {
2151
return this->x == other.x && this->y == other.y;
2252
}
2353

54+
[[nodiscard]] Vec2 mul(const Vec2& other) const {
55+
return {this->x * other.x, this->y * other.y};
56+
}
57+
58+
[[nodiscard]] Vec2 div(const Vec2& other) const {
59+
return {this->x / other.x, this->y / other.y};
60+
}
61+
62+
[[nodiscard]] Vec2 mod(const Vec2& other) const {
63+
if constexpr (std::floating_point<P>) {
64+
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y)};
65+
} else {
66+
return {this->x % other.x, this->y % other.y};
67+
}
68+
}
69+
70+
[[nodiscard]] Vec2 abs() const {
71+
return {std::abs(this->x), std::abs(this->y)};
72+
}
73+
2474
[[nodiscard]] static constexpr Vec2 zero() {
2575
return {{}, {}};
2676
}
@@ -52,14 +102,66 @@ struct Vec3 {
52102
return {this->x + other.x, this->y + other.y, this->z + other.z};
53103
}
54104

105+
void operator+=(const Vec3& other) {
106+
this->x += other.x;
107+
this->y += other.y;
108+
this->z += other.z;
109+
}
110+
55111
[[nodiscard]] Vec3 operator-(const Vec3& other) const {
56112
return {this->x - other.x, this->y - other.y, this->z - other.z};
57113
}
58114

115+
void operator-=(const Vec3& other) {
116+
this->x -= other.x;
117+
this->y -= other.y;
118+
this->z -= other.z;
119+
}
120+
121+
[[nodiscard]] Vec3 operator*(Arithmetic auto scalar) const {
122+
return {this->x * scalar, this->y * scalar, this->z * scalar};
123+
}
124+
125+
void operator*=(Arithmetic auto scalar) {
126+
this->x *= scalar;
127+
this->y *= scalar;
128+
this->z *= scalar;
129+
}
130+
131+
[[nodiscard]] Vec3 operator/(Arithmetic auto scalar) const {
132+
return {this->x / scalar, this->y / scalar, this->z / scalar};
133+
}
134+
135+
void operator/=(Arithmetic auto scalar) {
136+
this->x /= scalar;
137+
this->y /= scalar;
138+
this->z /= scalar;
139+
}
140+
59141
[[nodiscard]] bool operator==(const Vec3& other) const {
60142
return this->x == other.x && this->y == other.y && this->z == other.z;
61143
}
62144

145+
[[nodiscard]] Vec3 mul(const Vec3& other) const {
146+
return {this->x * other.x, this->y * other.y, this->z * other.z};
147+
}
148+
149+
[[nodiscard]] Vec3 div(const Vec3& other) const {
150+
return {this->x / other.x, this->y / other.y, this->z / other.z};
151+
}
152+
153+
[[nodiscard]] Vec3 mod(const Vec3& other) const {
154+
if constexpr (std::floating_point<P>) {
155+
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y), std::fmod(this->z, other.z)};
156+
} else {
157+
return {this->x % other.x, this->y % other.y, this->z % other.z};
158+
}
159+
}
160+
161+
[[nodiscard]] Vec3 abs() const {
162+
return {std::abs(this->x), std::abs(this->y), std::abs(this->z)};
163+
}
164+
63165
[[nodiscard]] static constexpr Vec3 zero() {
64166
return {{}, {}, {}};
65167
}
@@ -92,14 +194,70 @@ struct Vec4 {
92194
return {this->x + other.x, this->y + other.y, this->z + other.z, this->w + other.w};
93195
}
94196

197+
void operator+=(const Vec4& other) {
198+
this->x += other.x;
199+
this->y += other.y;
200+
this->z += other.z;
201+
this->w += other.w;
202+
}
203+
95204
[[nodiscard]] Vec4 operator-(const Vec4& other) const {
96205
return {this->x - other.x, this->y - other.y, this->z - other.z, this->w - other.w};
97206
}
98207

208+
void operator-=(const Vec4& other) {
209+
this->x -= other.x;
210+
this->y -= other.y;
211+
this->z -= other.z;
212+
this->w -= other.w;
213+
}
214+
215+
[[nodiscard]] Vec4 operator*(Arithmetic auto scalar) const {
216+
return {this->x * scalar, this->y * scalar, this->z * scalar, this->w * scalar};
217+
}
218+
219+
void operator*=(Arithmetic auto scalar) {
220+
this->x *= scalar;
221+
this->y *= scalar;
222+
this->z *= scalar;
223+
this->w *= scalar;
224+
}
225+
226+
[[nodiscard]] Vec4 operator/(Arithmetic auto scalar) const {
227+
return {this->x / scalar, this->y / scalar, this->z / scalar, this->w / scalar};
228+
}
229+
230+
void operator/=(Arithmetic auto scalar) {
231+
this->x /= scalar;
232+
this->y /= scalar;
233+
this->z /= scalar;
234+
this->w /= scalar;
235+
}
236+
99237
[[nodiscard]] bool operator==(const Vec4& other) const {
100238
return this->x == other.x && this->y == other.y && this->z == other.z && this->w == other.w;
101239
}
102240

241+
[[nodiscard]] Vec4 mul(const Vec4& other) const {
242+
return {this->x * other.x, this->y * other.y, this->z * other.z, this->z * other.z};
243+
}
244+
245+
[[nodiscard]] Vec4 div(const Vec4& other) const {
246+
return {this->x / other.x, this->y / other.y, this->z / other.z, this->w / other.w};
247+
}
248+
249+
[[nodiscard]] Vec4 mod(const Vec4& other) const {
250+
if constexpr (std::floating_point<P>) {
251+
return {std::fmod(this->x, other.x), std::fmod(this->y, other.y), std::fmod(this->z, other.z), std::fmod(this->w, other.w)};
252+
} else {
253+
return {this->x % other.x, this->y % other.y, this->z % other.z, this->w % other.w};
254+
}
255+
}
256+
257+
[[nodiscard]] Vec4 abs() const {
258+
return {std::abs(this->x), std::abs(this->y), std::abs(this->z), std::abs(this->w)};
259+
}
260+
103261
[[nodiscard]] static constexpr Vec4 zero() {
104262
return {{}, {}, {}, {}};
105263
}

0 commit comments

Comments
 (0)