ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
geometry.h
Go to the documentation of this file.
1
8
9#ifndef ASW_GEOMETRY_H
10#define ASW_GEOMETRY_H
11
12#include <cmath>
13
14namespace asw {
15
21template <typename T> class Vec2 {
22public:
25 Vec2() = default;
26
32 Vec2(T x, T y)
33 : x(x)
34 , y(y)
35 {
36 }
37
42 T angle(const Vec2& other) const
43 {
44 return std::atan2(y - other.y, x - other.x);
45 }
46
51 T angle() const
52 {
53 if (x == 0 && y == 0) {
54 return 0;
55 }
56 return std::atan2(y, x);
57 }
58
63 T distance(const Vec2& other) const
64 {
65 return std::hypot(x - other.x, y - other.y);
66 }
67
73 T dot(const Vec2& other) const
74 {
75 return (x * other.x) + (y * other.y);
76 }
77
83 T cross(const Vec2& other) const
84 {
85 return (x * other.y) - (y * other.x);
86 }
87
92 T magnitude() const
93 {
94 return std::sqrt((x * x) + (y * y));
95 }
96
102 Vec2 operator+(const Vec2& other) const
103 {
104 return Vec2(x + other.x, y + other.y);
105 }
106
112 Vec2 operator-(const Vec2& other) const
113 {
114 return Vec2(x - other.x, y - other.y);
115 }
116
122 Vec2 operator*(const T scalar) const
123 {
124 return Vec2(x * scalar, y * scalar);
125 }
126
132 Vec2 operator/(const T scalar) const
133 {
134 return Vec2(x / scalar, y / scalar);
135 }
136
142 Vec2 operator%(const T scalar) const
143 {
144 return Vec2(x % scalar, y % scalar);
145 }
146
152 Vec2& operator+=(const Vec2& other)
153 {
154 x += other.x;
155 y += other.y;
156 return *this;
157 }
158
164 Vec2& operator-=(const Vec2& other)
165 {
166 x -= other.x;
167 y -= other.y;
168 return *this;
169 }
170
176 Vec2& operator*=(const T scalar)
177 {
178 x *= scalar;
179 y *= scalar;
180 return *this;
181 }
182
188 Vec2& operator/=(const T scalar)
189 {
190 x /= scalar;
191 y /= scalar;
192 return *this;
193 }
194
200 Vec2& operator%=(const T scalar)
201 {
202 x %= scalar;
203 y %= scalar;
204 return *this;
205 }
206
212 bool operator==(const Vec2& other) const = default;
213
215 T x { 0 };
216
218 T y { 0 };
219};
220
226template <typename T> class Vec3 {
227public:
230 Vec3() = default;
231
238 Vec3(T x, T y, T z)
239 : x(x)
240 , y(y)
241 , z(z)
242 {
243 }
244
249 T angle(const Vec3& other) const
250 {
251 T dot_product = dot(other);
252 T magnitudes = magnitude() * other.magnitude();
253 return std::acos(dot_product / magnitudes);
254 }
255
260 T distance(const Vec3& other) const
261 {
262 return std::sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y)
263 + (z - other.z) * (z - other.z));
264 }
265
271 T dot(const Vec3& other) const
272 {
273 return (x * other.x) + (y * other.y) + (z * other.z);
274 }
275
281 Vec3 cross(const Vec3& other) const
282 {
283 return Vec3((y * other.z) - (z * other.y), (z * other.x) - (x * other.z),
284 (x * other.y) - (y * other.x));
285 }
286
291 T magnitude() const
292 {
293 return std::sqrt((x * x) + (y * y) + (z * z));
294 }
295
301 Vec3 operator+(const Vec3& other) const
302 {
303 return Vec3(x + other.x, y + other.y, z + other.z);
304 }
305
311 Vec3 operator-(const Vec3& other) const
312 {
313 return Vec3(x - other.x, y - other.y, z - other.z);
314 }
315
321 Vec3 operator*(const T scalar) const
322 {
323 return Vec3(x * scalar, y * scalar, z * scalar);
324 }
325
331 Vec3 operator/(const T scalar) const
332 {
333 return Vec3(x / scalar, y / scalar, z / scalar);
334 }
335
341 Vec3 operator%(const T scalar) const
342 {
343 return Vec3(x % scalar, y % scalar, z % scalar);
344 }
345
351 Vec3& operator+=(const Vec3& other)
352 {
353 x += other.x;
354 y += other.y;
355 z += other.z;
356 return *this;
357 }
358
364 Vec3& operator-=(const Vec3& other)
365 {
366 x -= other.x;
367 y -= other.y;
368 z -= other.z;
369 return *this;
370 }
371
377 Vec3& operator*=(const T scalar)
378 {
379 x *= scalar;
380 y *= scalar;
381 z *= scalar;
382 return *this;
383 }
384
390 Vec3& operator/=(const T scalar)
391 {
392 x /= scalar;
393 y /= scalar;
394 z /= scalar;
395 return *this;
396 }
397
403 Vec3& operator%=(const T scalar)
404 {
405 x %= scalar;
406 y %= scalar;
407 z %= scalar;
408 return *this;
409 }
410
416 bool operator==(const Vec3& other) const = default;
417
419 T x { 0 };
420
422 T y { 0 };
423
425 T z { 0 };
426};
427
433template <typename T> class Quad {
434public:
438 : position(0, 0)
439 , size(0, 0)
440 {
441 }
442
450 , size(size)
451 {
452 }
453
461 Quad(T x, T y, T width, T height)
462 : position(x, y)
463 , size(width, height)
464 {
465 }
466
472 void set_position(T x, T y)
473 {
474 position = Vec2<T>(x, y);
475 }
476
482 void set_size(T width, T height)
483 {
484 size = Vec2<T>(width, height);
485 }
486
492 {
493 return Vec2<T>(position.x + (size.x / 2.0F), position.y + (size.y / 2.0F));
494 }
495
501 bool contains(const Vec2<T>& point) const
502 {
503 return point.x >= position.x && point.x <= position.x + size.x && point.y >= position.y
504 && point.y <= position.y + size.y;
505 }
506
512 bool contains(T x, T y) const
513 {
514 return x >= position.x && x <= position.x + size.x && y >= position.y
515 && y <= position.y + size.y;
516 }
517
523 bool collides(const Quad& other) const
524 {
525 bool is_outside = position.x + size.x <= other.position.x || // a is left of b
526 other.position.x + other.size.x <= position.x || // b is left of a
527 position.y + size.y <= other.position.y || // a is above b
528 other.position.y + other.size.y <= position.y; // b is above a
529
530 return !is_outside;
531 }
532
533 // Collision
534 bool collides_bottom(const Quad& other) const
535 {
536 return position.y < other.position.y + other.size.y
537 && position.y + size.y > other.position.y + other.size.y;
538 }
539
540 bool collides_top(const Quad& other) const
541 {
542 return position.y + size.y > other.position.y && position.y < other.position.y;
543 }
544
545 bool collides_left(const Quad& other) const
546 {
547 return position.x + size.x > other.position.x && position.x < other.position.x;
548 }
549
550 bool collides_right(const Quad& other) const
551 {
552 return position.x < other.position.x + other.size.x
553 && position.x + size.x > other.position.x + other.size.x;
554 }
555
561 Quad operator+(const Quad<T>& quad) const
562 {
563 return Quad(position + quad.position, size + quad.size);
564 }
565
571 Quad operator-(const Quad<T>& quad) const
572 {
573 return Quad(position - quad.position, size - quad.size);
574 }
575
581 Quad operator*(const T scalar) const
582 {
583 return Quad(position * scalar, size * scalar);
584 }
585
591 Quad operator/(const T scalar) const
592 {
593 return Quad(position / scalar, size / scalar);
594 }
595
598
601};
602
610
611} // namespace asw
612
613#endif // ASW_GEOMETRY_H
A 2D rectangle in space.
Definition geometry.h:433
Quad operator-(const Quad< T > &quad) const
Subtract a vector from the rectangle.
Definition geometry.h:571
bool collides_top(const Quad &other) const
Definition geometry.h:540
Quad operator+(const Quad< T > &quad) const
Add a vector to the rectangle.
Definition geometry.h:561
Quad operator/(const T scalar) const
Divide the rectangle by a scalar.
Definition geometry.h:591
Quad()
Default constructor for the Quad class.
Definition geometry.h:437
bool collides(const Quad &other) const
Check if a rectangle is inside the rectangle.
Definition geometry.h:523
Quad(const Vec2< T > &position, const Vec2< T > &size)
Constructor for the Quad class.
Definition geometry.h:448
bool collides_left(const Quad &other) const
Definition geometry.h:545
Vec2< T > get_center() const
Get center of the rectangle.
Definition geometry.h:491
bool collides_bottom(const Quad &other) const
Definition geometry.h:534
Quad(T x, T y, T width, T height)
Constructor for the Quad class.
Definition geometry.h:461
Vec2< T > size
The size of the rectangle.
Definition geometry.h:600
Vec2< T > position
The position of the rectangle.
Definition geometry.h:597
void set_size(T width, T height)
Set the size of the rectangle.
Definition geometry.h:482
bool collides_right(const Quad &other) const
Definition geometry.h:550
Quad operator*(const T scalar) const
Multiply the rectangle by a scalar.
Definition geometry.h:581
bool contains(const Vec2< T > &point) const
Check if a point is inside the rectangle.
Definition geometry.h:501
bool contains(T x, T y) const
Check if coordinates are inside the rectangle.
Definition geometry.h:512
void set_position(T x, T y)
Set the position of the rectangle.
Definition geometry.h:472
A 2D vector in space.
Definition geometry.h:21
T cross(const Vec2 &other) const
Calculate the cross product of two vectors.
Definition geometry.h:83
bool operator==(const Vec2 &other) const =default
Equality operator for the Vec2 class.
Vec2 operator+(const Vec2 &other) const
Addition operator for the Vec2 class.
Definition geometry.h:102
Vec2 operator/(const T scalar) const
Division operator for the Vec2 class.
Definition geometry.h:132
Vec2 & operator-=(const Vec2 &other)
Subtraction assignment operator for the Vec2 class.
Definition geometry.h:164
Vec2()=default
Default constructor for the Vec2 class.
Vec2 operator%(const T scalar) const
Modulo the Vec2 object by a scalar.
Definition geometry.h:142
Vec2 & operator%=(const T scalar)
Modulo assignment operator for the Vec2 class.
Definition geometry.h:200
T y
The y component of the vector.
Definition geometry.h:218
T x
The x component of the vector.
Definition geometry.h:215
Vec2 operator*(const T scalar) const
Multiplication operator for the Vec2 class.
Definition geometry.h:122
T distance(const Vec2 &other) const
Get distance between two vectors.
Definition geometry.h:63
Vec2 & operator/=(const T scalar)
Division assignment operator for the Vec2 class.
Definition geometry.h:188
Vec2(T x, T y)
Constructor for the Vec2 class.
Definition geometry.h:32
Vec2 & operator*=(const T scalar)
Multiplication assignment operator for the Vec2 class.
Definition geometry.h:176
T angle(const Vec2 &other) const
Get angle of two vectors.
Definition geometry.h:42
T magnitude() const
Calculate the magnitude of the vector.
Definition geometry.h:92
Vec2 operator-(const Vec2 &other) const
Subtraction operator for the Vec2 class.
Definition geometry.h:112
T angle() const
Calculate the angle of the vector.
Definition geometry.h:51
Vec2 & operator+=(const Vec2 &other)
Addition assignment operator for the Vec2 class.
Definition geometry.h:152
T dot(const Vec2 &other) const
Calculate the dot product of two vectors.
Definition geometry.h:73
A 3D vector in space.
Definition geometry.h:226
T z
The z component of the vector.
Definition geometry.h:425
Vec3()=default
Default constructor for the Vec3 class.
Vec3(T x, T y, T z)
Constructor for the Vec3 class.
Definition geometry.h:238
Vec3 operator/(const T scalar) const
Division operator for the Vec3 class.
Definition geometry.h:331
T x
The x component of the vector.
Definition geometry.h:419
Vec3 cross(const Vec3 &other) const
Calculate the cross product of two vectors.
Definition geometry.h:281
Vec3 & operator-=(const Vec3 &other)
Subtraction assignment operator for the Vec3 class.
Definition geometry.h:364
T dot(const Vec3 &other) const
Calculate the dot product of two vectors.
Definition geometry.h:271
Vec3 operator%(const T scalar) const
Modulo the Vec3 object by a scalar.
Definition geometry.h:341
Vec3 operator+(const Vec3 &other) const
Addition operator for the Vec3 class.
Definition geometry.h:301
T magnitude() const
Calculate the magnitude of the vector.
Definition geometry.h:291
T y
The y component of the vector.
Definition geometry.h:422
T angle(const Vec3 &other) const
Get angle between two vectors (in 3D space).
Definition geometry.h:249
Vec3 & operator*=(const T scalar)
Multiplication assignment operator for the Vec3 class.
Definition geometry.h:377
Vec3 & operator/=(const T scalar)
Division assignment operator for the Vec3 class.
Definition geometry.h:390
bool operator==(const Vec3 &other) const =default
Equality operator for the Vec3 class.
Vec3 operator-(const Vec3 &other) const
Subtraction operator for the Vec3 class.
Definition geometry.h:311
Vec3 & operator+=(const Vec3 &other)
Addition assignment operator for the Vec3 class.
Definition geometry.h:351
T distance(const Vec3 &other) const
Get distance between two vectors.
Definition geometry.h:260
Vec3 & operator%=(const T scalar)
Modulo assignment operator for the Vec3 class.
Definition geometry.h:403
Vec3 operator*(const T scalar) const
Multiplication operator for the Vec3 class.
Definition geometry.h:321