Yasiu Math 1
Collection of mathematic functions that help create game mechanics and procedural tools
Loading...
Searching...
No Matches
YasiuMathDataTypes.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Grzegorz Krug.
3 * All Rights Reserved.
4 */
5
7
8#pragma once
9
10
11#include "Constants.h"
12
13
14#include <cmath>
15#include <utility>
16
17
19
20
22namespace YasiuMath {
24 namespace Types {
32 template<typename T>
33 struct Point {
35 T x = 0;
37 T y = 0;
38
39 Point( T x, T y )
40 : x(x), y(y) {}
41
42 Point( std::pair<T, T> point )
43 : x(point.first), y(point.second) {}
44
47 {
48 if ( x == 0.f ) {
49 return y;
50 }
51 else {
52 return static_cast<T>(atan2(y, x));
53 }
54 }
55
56 T fastAngleModded()
57 {
58 // return fastAngle();
59 return static_cast<T>(fmod(fastAngle(), static_cast<T>(YasiuMath::Constants::Y_PI)));
60 }
61 };
62
63
68 template<typename U, typename T>
69 struct IndexedPair {
71 U index = 0;
73 T first = 0;
75 T second = 0;
76
77 IndexedPair() = default;
78
79 IndexedPair( U index, T first, T second )
81
82 Point<T> operator-( const IndexedPair<U, T>& other ) const
83 {
84 return Point<T>{first - other.first, second - other.second};
85 }
86
87 IndexedPair<U, T> subtract( const IndexedPair<U, T>& other ) const
88 {
89 return IndexedPair<U, T>{index, first - other.first, second - other.second};
90 }
91
92 bool operator==( const IndexedPair<U, T>& other ) const
93 {
94 return index == other.index;
95 }
96 };
97
98
104 template<typename T>
105 struct PointAngle {
106 int index = 0;
107 T angle = 0;
108
109 PointAngle( const int& index, T angle )
110 : index(index), angle(angle) {}
111
112 PointAngle( const IndexedPair<int, T>& point )
113 {
114 index = point.index;
115 angle = atan2(point.second, point.first);
116 }
117
118 PointAngle( const int& ind, const T& x, const T& y )
119 : index(ind)
120 {
121 angle = atan2(y, x);
122 }
123
124 bool operator<( const PointAngle<T>& other ) const
125 {
126 return angle < other.angle;
127 }
128
129 bool operator>( const PointAngle<T>& other ) const
130 {
131 return angle > other.angle;
132 }
133 };
134
135
141 template<typename T>
142 struct Vec3 {
144 T X = 0;
146 T Y = 0;
148 T Z = 0;
149
150 Vec3() {};
151
152 Vec3( T in )
153 : X(in), Y(in), Z(in) {};
154
155 Vec3( T xin, T yin, T zin )
156 : X(xin), Y(yin), Z(zin) {};
157
158 template<typename U>
159 constexpr Vec3( const Vec3<U>& other )
160 : X(static_cast<T>(other.X)), Y(static_cast<T>(other.Y)), Z(static_cast<T>(other.Z)) {}
161
162
163 // Addition
164 Vec3 operator+( const Vec3& other ) const
165 {
166 return Vec3(X + other.X, Y + other.Y, Z + other.Z);
167 }
168
169 Vec3& operator+=( const Vec3& other )
170 {
171 X += other.X;
172 Y += other.Y;
173 Z += other.Z;
174 return *this;
175 }
176
177 // Subtraction
178 Vec3 operator-( const Vec3& other ) const
179 {
180 return Vec3(X - other.X, Y - other.Y, Z - other.Z);
181 }
182
183 Vec3& operator-=( const Vec3& other )
184 {
185 X -= other.X;
186 Y -= other.Y;
187 Z -= other.Z;
188 return *this;
189 }
190
191 // Scalar multiplication
192 Vec3 operator*( T scalar ) const
193 {
194 return Vec3(X * scalar, Y * scalar, Z * scalar);
195 }
196
197 Vec3 operator*( Vec3 other ) const
198 {
199 return Vec3(X * other.X, Y * other.Y, Z * other.Z);
200 }
201
202 Vec3& operator*=( T scalar )
203 {
204 X *= scalar;
205 Y *= scalar;
206 Z *= scalar;
207 return *this;
208 }
209
210 // Scalar division
211 Vec3 operator/( T scalar ) const
212 {
213 return Vec3(X / scalar, Y / scalar, Z / scalar);
214 }
215
216 Vec3& operator/=( T scalar )
217 {
218 X /= scalar;
219 Y /= scalar;
220 Z /= scalar;
221 return *this;
222 }
223
224 Vec3 operator/( const Vec3& other ) const
225 {
226 return Vec3(X / other.X, Y / other.Y, Z / other.Z);
227 }
228
229 Vec3& operator/=( const Vec3& other )
230 {
231 X /= other.X;
232 Y /= other.Y;
233 Z /= other.Z;
234 return *this;
235 }
236
238 bool operator==( const Vec3& other ) const
239 {
240 return X == other.X && Y == other.Y && Z == other.Z;
241 }
242
244 bool operator!=( const Vec3& other ) const
245 {
246 return X != other.X || Y != other.Y || Z != other.Z;
247 }
248
250 bool operator>( const T value ) const
251 {
252 return Length() > value;
253 }
254
256 bool operator<( const T value ) const
257 {
258 return Length() < value;
259 }
260
262 bool operator>( const Vec3& other ) const
263 {
264 return LengthSquared() > other.LengthSquared();
265 }
266
268 bool operator<( const Vec3& other ) const
269 {
270 return LengthSquared() < other.LengthSquared();
271 }
272
274 T Dot( const Vec3& other ) const
275 {
276 return X * other.X + Y * other.Y + Z * other.Z;
277 }
278
280 Vec3 Cross( const Vec3& other ) const
281 {
282 return Vec3(Y * other.Z - Z * other.Y, Z * other.X - X * other.Z, X * other.Y - Y * other.X);
283 }
284
287 {
288 return X * X + Y * Y + Z * Z;
289 }
290
292 T Length() const
293 {
294 return static_cast<T>(std::sqrt(LengthSquared()));
295 }
296
298 Vec3 Abs() const
299 {
300 return Vec3{fabs(X), fabs(Y), fabs(Z)};
301 }
302
304 T Sum() const
305 {
306 return X + Y + Z;
307 }
308
310 T AbsSum() const
311 {
312 return fabs(X) + fabs(Y) + fabs(Z);
313 }
314
316 Vec3 Normalize() const
317 {
318 // Vec3 other = *this;
319 // other.NormalizeInPlace();
320 return Vec3(*this).NormalizeInPlace();
321 }
322
325 {
326 T len = Length();
327 if ( len != 0 ) {
328 X /= len;
329 Y /= len;
330 Z /= len;
331 }
332 return *this;
333 }
334
336 bool IsNearly0( const T EPS = YasiuMath::Constants::EPSILON ) const
337 {
338 return AbsSum() < EPS;
339 }
340 };
341 }
342}
constexpr double Y_PI
Famous PI value.
Definition Constants.h:14
constexpr double EPSILON
Value used for number stability check.
Definition Constants.h:17
Generic Types used in library.
Definition YasiuMathDataTypes.h:24
Categorized name spaces.
Definition Constants.h:10
Helper object, for point description in algorithm. Index and 2 values.
Definition YasiuMathDataTypes.h:69
T second
Definition YasiuMathDataTypes.h:75
U index
Definition YasiuMathDataTypes.h:71
T first
Definition YasiuMathDataTypes.h:73
Pair-like type, Helper for point description in ConvexHull algorithm.
Definition YasiuMathDataTypes.h:33
T x
Definition YasiuMathDataTypes.h:35
T fastAngle()
Get Angle from X/Y.
Definition YasiuMathDataTypes.h:46
T y
Definition YasiuMathDataTypes.h:37
Structure reflecting vector of size 3, Templated Type.
Definition YasiuMathDataTypes.h:142
bool operator==(const Vec3 &other) const
All elements must match.
Definition YasiuMathDataTypes.h:238
bool operator<(const Vec3 &other) const
Compare magnitudes.
Definition YasiuMathDataTypes.h:268
Vec3 Cross(const Vec3 &other) const
Cross product of 2 vectors.
Definition YasiuMathDataTypes.h:280
float Z
Definition YasiuMathDataTypes.h:148
Vec3 Normalize() const
Return copy of normalized vector.
Definition YasiuMathDataTypes.h:316
bool operator>(const T value) const
Compare magnitudes.
Definition YasiuMathDataTypes.h:250
T Sum() const
Sum inner elements.
Definition YasiuMathDataTypes.h:304
T LengthSquared() const
Length^2.
Definition YasiuMathDataTypes.h:286
float X
Definition YasiuMathDataTypes.h:144
Vec3 & NormalizeInPlace()
Normalize this vector.
Definition YasiuMathDataTypes.h:324
bool operator!=(const Vec3 &other) const
OnlY 1 can be different.
Definition YasiuMathDataTypes.h:244
bool IsNearly0(const T EPS=YasiuMath::Constants::EPSILON) const
Check if vector has any element other than 0, uses YasiuMath::Constants::EPSILON.
Definition YasiuMathDataTypes.h:336
T Dot(const Vec3 &other) const
Dot product of 2 vectors.
Definition YasiuMathDataTypes.h:274
bool operator>(const Vec3 &other) const
Compare magnitudes.
Definition YasiuMathDataTypes.h:262
float Y
Definition YasiuMathDataTypes.h:146
Vec3 Abs() const
Vector with absolute values.
Definition YasiuMathDataTypes.h:298
bool operator<(const T value) const
Compare magnitudes.
Definition YasiuMathDataTypes.h:256
T Length() const
Default Pythagoras Length.
Definition YasiuMathDataTypes.h:292
T AbsSum() const
Sum of absolute values of components.
Definition YasiuMathDataTypes.h:310