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
21 template <typename T>
22 class Vec2 {
23 public:
26 Vec2() : x(0), y(0) {}
27
33 Vec2(T x, T y) : x(x), y(y) {}
34
39 T angle(const Vec2& other) const {
40 return std::atan2(y - other.y, x - other.x);
41 }
42
47 T angle() const {
48 if (x == 0 && y == 0) {
49 return 0;
50 }
51 return std::atan2(y, x);
52 }
53
58 T distance(const Vec2& other) const {
59 return std::hypotf(x - other.x, y - other.y);
60 }
61
67 T dot(const Vec2& other) const { return (x * other.x) + (y * other.y); }
68
74 T cross(const Vec2& other) const { return (x * other.y) - (y * other.x); }
75
80 T magnitude() const { return std::sqrt((x * x) + (y * y)); }
81
87 Vec2 operator+(const Vec2& other) const {
88 return Vec2(x + other.x, y + other.y);
89 }
90
96 Vec2 operator-(const Vec2& other) const {
97 return Vec2(x - other.x, y - other.y);
98 }
99
105 Vec2 operator*(const T scalar) const {
106 return Vec2(x * scalar, y * scalar);
107 }
108
114 Vec2 operator/(const T scalar) const {
115 return Vec2(x / scalar, y / scalar);
116 }
117
123 Vec2& operator+=(const Vec2& other) {
124 x += other.x;
125 y += other.y;
126 return *this;
127 }
128
134 Vec2& operator-=(const Vec2& other) {
135 x -= other.x;
136 y -= other.y;
137 return *this;
138 }
139
145 Vec2& operator*=(const T scalar) {
146 x *= scalar;
147 y *= scalar;
148 return *this;
149 }
150
156 Vec2& operator/=(const T scalar) {
157 x /= scalar;
158 y /= scalar;
159 return *this;
160 }
161
167 bool operator==(const Vec2& other) const {
168 return x == other.x && y == other.y;
169 }
170
176 bool operator!=(const Vec2& other) const {
177 return x != other.x || y != other.y;
178 }
179
181 T x{0};
182
184 T y{0};
185 };
186
192 template <typename T>
193 class Vec3 {
194 public:
197 Vec3() : x(0), y(0), z(0) {}
198
205 Vec3(T x, T y, T z) : x(x), y(y), z(z) {}
206
211 T angle(const Vec3& other) const {
212 T dotProduct = dot(other);
213 T magnitudes = magnitude() * other.magnitude();
214 return std::acos(dotProduct / magnitudes);
215 }
216
221 T distance(const Vec3& other) const {
222 return std::sqrt((x - other.x) * (x - other.x) +
223 (y - other.y) * (y - other.y) +
224 (z - other.z) * (z - other.z));
225 }
226
232 T dot(const Vec3& other) const {
233 return (x * other.x) + (y * other.y) + (z * other.z);
234 }
235
241 Vec3 cross(const Vec3& other) const {
242 return Vec3((y * other.z) - (z * other.y), (z * other.x) - (x * other.z),
243 (x * other.y) - (y * other.x));
244 }
245
250 T magnitude() const { return std::sqrt((x * x) + (y * y) + (z * z)); }
251
257 Vec3 operator+(const Vec3& other) const {
258 return Vec3(x + other.x, y + other.y, z + other.z);
259 }
260
266 Vec3 operator-(const Vec3& other) const {
267 return Vec3(x - other.x, y - other.y, z - other.z);
268 }
269
275 Vec3 operator*(const T scalar) const {
276 return Vec3(x * scalar, y * scalar, z * scalar);
277 }
278
284 Vec3 operator/(const T scalar) const {
285 return Vec3(x / scalar, y / scalar, z / scalar);
286 }
287
293 Vec3& operator+=(const Vec3& other) {
294 x += other.x;
295 y += other.y;
296 z += other.z;
297 return *this;
298 }
299
305 Vec3& operator-=(const Vec3& other) {
306 x -= other.x;
307 y -= other.y;
308 z -= other.z;
309 return *this;
310 }
311
317 Vec3& operator*=(const T scalar) {
318 x *= scalar;
319 y *= scalar;
320 z *= scalar;
321 return *this;
322 }
323
329 Vec3& operator/=(const T scalar) {
330 x /= scalar;
331 y /= scalar;
332 z /= scalar;
333 return *this;
334 }
335
341 bool operator==(const Vec3& other) const {
342 return x == other.x && y == other.y && z == other.z;
343 }
344
350 bool operator!=(const Vec3& other) const {
351 return x != other.x || y != other.y || z != other.z;
352 }
353
355 T x{0};
356
358 T y{0};
359
361 T z{0};
362 };
363
369 template <typename T>
370 class Quad {
371 public:
374 Quad() : position(0, 0), size(0, 0) {}
375
383
391 Quad(T x, T y, T width, T height) : position(x, y), size(width, height) {}
392
398 void setPosition(T x, T y) { position = Vec2<T>(x, y); }
399
405 void setSize(T width, T height) { size = Vec2<T>(width, height); }
406
412 return Vec2<T>(position.x + (size.x / 2.0F),
413 position.y + (size.y / 2.0F));
414 }
415
421 bool contains(const Vec2<T>& point) const {
422 return point.x >= position.x && point.x <= position.x + size.x &&
423 point.y >= position.y && point.y <= position.y + size.y;
424 }
425
431 bool contains(T x, T y) const {
432 return x >= position.x && x <= position.x + size.x && y >= position.y &&
433 y <= position.y + size.y;
434 }
435
441 bool collides(const Quad& other) const {
442 bool is_outside =
443 position.x + size.x <= other.position.x || // a is left of b
444 other.position.x + other.size.x <= position.x || // b is left of a
445 position.y + size.y <= other.position.y || // a is above b
446 other.position.y + other.size.y <= position.y; // b is above a
447
448 return !is_outside;
449 }
450
451 // Collision
452 bool collidesBottom(const Quad& other) const {
453 return position.y < other.position.y + other.size.y &&
454 position.y + size.y > other.position.y + other.size.y;
455 }
456
457 bool collidesTop(const Quad& other) const {
458 return position.y + size.y > other.position.y &&
459 position.y < other.position.y;
460 }
461
462 bool collidesLeft(const Quad& other) const {
463 return position.x + size.x > other.position.x &&
464 position.x < other.position.x;
465 }
466
467 bool collidesRight(const Quad& other) const {
468 return position.x < other.position.x + other.size.x &&
469 position.x + size.x > other.position.x + other.size.x;
470 }
471
477 Quad operator+(const Quad<T>& quad) const {
478 return Quad(position + quad.position, size + quad.size);
479 }
480
486 Quad operator-(const Quad<T>& quad) const {
487 return Quad(position - quad.position, size - quad.size);
488 }
489
495 Quad operator*(const T scalar) const {
496 return Quad(position * scalar, size * scalar);
497 }
498
504 Quad operator/(const T scalar) const {
505 return Quad(position / scalar, size / scalar);
506 }
507
510
513 };
514
515} // namespace asw
516
517#endif // ASW_GEOMETRY_H
A 2D rectangle in space.
Definition geometry.h:370
Quad operator-(const Quad< T > &quad) const
Subtract a vector from the rectangle.
Definition geometry.h:486
bool collidesBottom(const Quad &other) const
Definition geometry.h:452
Quad operator+(const Quad< T > &quad) const
Add a vector to the rectangle.
Definition geometry.h:477
Quad operator/(const T scalar) const
Divide the rectangle by a scalar.
Definition geometry.h:504
bool collidesLeft(const Quad &other) const
Definition geometry.h:462
Vec2< T > getCenter() const
Get center of the rectangle.
Definition geometry.h:411
Quad()
Default constructor for the Quad class.
Definition geometry.h:374
bool collides(const Quad &other) const
Check if a rectangle is inside the rectangle.
Definition geometry.h:441
Quad(const Vec2< T > &position, const Vec2< T > &size)
Constructor for the Quad class.
Definition geometry.h:381
Quad(T x, T y, T width, T height)
Constructor for the Quad class.
Definition geometry.h:391
void setSize(T width, T height)
Set the size of the rectangle.
Definition geometry.h:405
bool collidesTop(const Quad &other) const
Definition geometry.h:457
Vec2< T > size
The size of the rectangle.
Definition geometry.h:512
Vec2< T > position
The position of the rectangle.
Definition geometry.h:509
Quad operator*(const T scalar) const
Multiply the rectangle by a scalar.
Definition geometry.h:495
void setPosition(T x, T y)
Set the position of the rectangle.
Definition geometry.h:398
bool contains(const Vec2< T > &point) const
Check if a point is inside the rectangle.
Definition geometry.h:421
bool collidesRight(const Quad &other) const
Definition geometry.h:467
bool contains(T x, T y) const
Check if coordinates are inside the rectangle.
Definition geometry.h:431
A 2D vector in space.
Definition geometry.h:22
T cross(const Vec2 &other) const
Calculate the cross product of two vectors.
Definition geometry.h:74
Vec2 operator+(const Vec2 &other) const
Addition operator for the Vec2 class.
Definition geometry.h:87
Vec2 operator/(const T scalar) const
Division operator for the Vec2 class.
Definition geometry.h:114
Vec2 & operator-=(const Vec2 &other)
Subtraction assignment operator for the Vec2 class.
Definition geometry.h:134
bool operator!=(const Vec2 &other) const
Inequality operator for the Vec2 class.
Definition geometry.h:176
T y
The y component of the vector.
Definition geometry.h:184
T x
The x component of the vector.
Definition geometry.h:181
Vec2 operator*(const T scalar) const
Multiplication operator for the Vec2 class.
Definition geometry.h:105
T distance(const Vec2 &other) const
Get distance between two vectors.
Definition geometry.h:58
Vec2 & operator/=(const T scalar)
Division assignment operator for the Vec2 class.
Definition geometry.h:156
Vec2(T x, T y)
Constructor for the Vec2 class.
Definition geometry.h:33
Vec2 & operator*=(const T scalar)
Multiplication assignment operator for the Vec2 class.
Definition geometry.h:145
T angle(const Vec2 &other) const
Get angle of two vectors.
Definition geometry.h:39
T magnitude() const
Calculate the magnitude of the vector.
Definition geometry.h:80
Vec2 operator-(const Vec2 &other) const
Subtraction operator for the Vec2 class.
Definition geometry.h:96
T angle() const
Calculate the angle of the vector.
Definition geometry.h:47
Vec2()
Default constructor for the Vec2 class.
Definition geometry.h:26
Vec2 & operator+=(const Vec2 &other)
Addition assignment operator for the Vec2 class.
Definition geometry.h:123
T dot(const Vec2 &other) const
Calculate the dot product of two vectors.
Definition geometry.h:67
bool operator==(const Vec2 &other) const
Equality operator for the Vec2 class.
Definition geometry.h:167
A 3D vector in space.
Definition geometry.h:193
T z
The z component of the vector.
Definition geometry.h:361
Vec3(T x, T y, T z)
Constructor for the Vec3 class.
Definition geometry.h:205
Vec3()
Default constructor for the Vec3 class.
Definition geometry.h:197
Vec3 operator/(const T scalar) const
Division operator for the Vec3 class.
Definition geometry.h:284
T x
The x component of the vector.
Definition geometry.h:355
Vec3 cross(const Vec3 &other) const
Calculate the cross product of two vectors.
Definition geometry.h:241
Vec3 & operator-=(const Vec3 &other)
Subtraction assignment operator for the Vec3 class.
Definition geometry.h:305
T dot(const Vec3 &other) const
Calculate the dot product of two vectors.
Definition geometry.h:232
Vec3 operator+(const Vec3 &other) const
Addition operator for the Vec3 class.
Definition geometry.h:257
T magnitude() const
Calculate the magnitude of the vector.
Definition geometry.h:250
T y
The y component of the vector.
Definition geometry.h:358
T angle(const Vec3 &other) const
Get angle between two vectors (in 3D space).
Definition geometry.h:211
Vec3 & operator*=(const T scalar)
Multiplication assignment operator for the Vec3 class.
Definition geometry.h:317
Vec3 & operator/=(const T scalar)
Division assignment operator for the Vec3 class.
Definition geometry.h:329
bool operator!=(const Vec3 &other) const
Inequality operator for the Vec3 class.
Definition geometry.h:350
Vec3 operator-(const Vec3 &other) const
Subtraction operator for the Vec3 class.
Definition geometry.h:266
bool operator==(const Vec3 &other) const
Equality operator for the Vec3 class.
Definition geometry.h:341
Vec3 & operator+=(const Vec3 &other)
Addition assignment operator for the Vec3 class.
Definition geometry.h:293
T distance(const Vec3 &other) const
Get distance between two vectors.
Definition geometry.h:221
Vec3 operator*(const T scalar) const
Multiplication operator for the Vec3 class.
Definition geometry.h:275
Definition random.cpp:5