39template <
typename T>
struct Vec2
77 Vec2(
const std::vector<T> &vec) :
x(0),
y(0)
81 throw std::invalid_argument(
"Vector must contain exactly two elements.");
97 return ((this->x == other_vec.
x) && (this->y == other_vec.
y));
110 return ((this->x != other_vec.
x) || (this->y != other_vec.
y));
141 out.
x = this->x / other_vec.
x;
142 out.
y = this->y / other_vec.
y;
159 out.
x = this->x * other_vec.
x;
160 out.
y = this->y * other_vec.
y;
176 out.
x = this->x + other_vec.
x;
177 out.
y = this->y + other_vec.
y;
193 out.
x = this->x - other_vec.
x;
194 out.
y = this->y - other_vec.
y;
209 return Vec2(
x * scalar,
y * scalar);
226 return Vec2(scalar * vec.
x, scalar * vec.
y);
241 return v1.
x * v2.
x + v1.
y * v2.
y;
251 return std::sqrt(
x *
x +
y *
y);
280template <
typename T>
struct Vec3
321 Vec3(
const std::vector<T> &vec) :
x(0),
y(0),
z(0)
325 throw std::invalid_argument(
326 "Vector must contain exactly three elements.");
343 return ((this->x == other_vec.
x) && (this->y == other_vec.
y) &&
344 (this->z == other_vec.
z));
357 return ((this->x != other_vec.
x) || (this->y != other_vec.
y) ||
358 (this->z != other_vec.
z));
390 out.
x = this->x / other_vec.
x;
391 out.
y = this->y / other_vec.
y;
392 out.
z = this->z / other_vec.
z;
409 out.
x = this->x * other_vec.
x;
410 out.
y = this->y * other_vec.
y;
411 out.
z = this->z * other_vec.
z;
427 out.
x = this->x + other_vec.
x;
428 out.
y = this->y + other_vec.
y;
429 out.
z = this->z + other_vec.
z;
445 out.
x = this->x - other_vec.
x;
446 out.
y = this->y - other_vec.
y;
447 out.
z = this->z - other_vec.
z;
462 return Vec3(
x * scalar,
y * scalar,
z * scalar);
479 return Vec3(scalar * vec.
x, scalar * vec.
y, scalar * vec.
z);
495 out.
x = v1.
y * v2.
z - v1.
z * v2.
y;
496 out.
y = v1.
z * v2.
x - v1.
x * v2.
z;
497 out.
z = v1.
x * v2.
y - v1.
y * v2.
x;
513 return v1.
x * v2.
x + v1.
y * v2.
y + v1.
z * v2.
z;
523 return std::sqrt(
x *
x +
y *
y +
z *
z);
563template <
typename T>
struct Vec4
607 Vec4(
const std::vector<T> &vec) :
a(0),
b(0),
c(0),
d(0)
611 throw std::invalid_argument(
"Vector must contain exactly four elements.");
629 return ((this->a == other_vec.
a) && (this->b == other_vec.
b) &&
630 (this->c == other_vec.
c) && (this->d == other_vec.
d));
643 return ((this->a != other_vec.
a) || (this->b != other_vec.
b) ||
644 (this->c != other_vec.
c) || (this->d != other_vec.
d));
677 out.
a = this->a / other_vec.
a;
678 out.
b = this->b / other_vec.
b;
679 out.
c = this->c / other_vec.
c;
680 out.
d = this->d / other_vec.
d;
697 out.
a = this->a * other_vec.
a;
698 out.
b = this->b * other_vec.
b;
699 out.
c = this->c * other_vec.
c;
700 out.
d = this->d * other_vec.
d;
716 out.
a = this->a + other_vec.
a;
717 out.
b = this->b + other_vec.
b;
718 out.
c = this->c + other_vec.
c;
719 out.
d = this->d + other_vec.
d;
735 out.
a = this->a - other_vec.
a;
736 out.
b = this->b - other_vec.
b;
737 out.
c = this->c - other_vec.
c;
738 out.
d = this->d - other_vec.
d;
753 return Vec4(
a * scalar,
b * scalar,
c * scalar,
d * scalar);
770 return Vec4(scalar * vec.
a, scalar * vec.
b, scalar * vec.
c, scalar * vec.
d);
788 return Vec4<T>(this->a + da, this->b + db, this->c + dc, this->d + dd);
803 return v1.
a * v2.
a + v1.
b * v2.
b + v1.
c * v2.
c + v1.
d * v2.
d;
816template <
typename T>
struct Mat
850 return this->vector[i * this->shape.
y + j];
866 return this->vector[i * this->shape.
y + j];
Definition algebra.hpp:28
Vec3< T > normalized_vec3(T x, T y, T z)
Constructs a normalized 3D vector.
Definition algebra.hpp:882
Mat class for basic manipulation of 2D matrices.
Definition algebra.hpp:817
Mat(Vec2< int > shape)
Constructor to initialize a matrix with a given shape.
Definition algebra.hpp:833
Vec2< int > shape
Dimensions of the matrix (rows x columns).
Definition algebra.hpp:820
T & operator()(int i, int j)
Access operator to get a reference to the element at (i, j).
Definition algebra.hpp:848
std::vector< T > vector
1D vector storing matrix elements in row-major order.
Definition algebra.hpp:818
const T & operator()(int i, int j) const
Const access operator to get the value of the element at (i, j).
Definition algebra.hpp:864
Vec2 class for basic manipulation of 2D vectors.
Definition algebra.hpp:40
T y
The x and y components of the vector.
Definition algebra.hpp:41
Vec2 operator/(const Vec2 &other_vec) const
Division operator.
Definition algebra.hpp:138
bool operator==(const Vec2 &other_vec) const
Equality operator.
Definition algebra.hpp:95
Vec2 operator+(const Vec2 &other_vec) const
Addition operator.
Definition algebra.hpp:173
T x
Definition algebra.hpp:41
bool operator!=(const Vec2 &other_vec) const
Inequality operator.
Definition algebra.hpp:108
void normalize()
Normalize the vector to have a magnitude of 1.
Definition algebra.hpp:260
Vec2 operator*(T scalar) const
Scalar multiplication (Vec2 * scalar).
Definition algebra.hpp:207
Vec2(const std::vector< T > &vec)
Constructs a Vec2 object from a std::vector.
Definition algebra.hpp:77
friend Vec2 operator*(T scalar, const Vec2 &vec)
Scalar multiplication (scalar * Vec2).
Definition algebra.hpp:224
Vec2 operator-(const Vec2 &other_vec) const
Subtraction operator.
Definition algebra.hpp:190
T magnitude() const
Calculate the magnitude (length) of the vector.
Definition algebra.hpp:249
friend float dot(const Vec2 v1, const Vec2 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:239
Vec2()
Default constructor initializing the vector to (0, 0).
Definition algebra.hpp:48
Vec2 operator*(const Vec2 &other_vec) const
Multiplication operator.
Definition algebra.hpp:156
Vec2(T x, T y)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:59
Vec2 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:122
Vec3 class for basic manipulation of 3D vectors.
Definition algebra.hpp:281
bool operator==(const Vec3 &other_vec) const
Equality operator.
Definition algebra.hpp:341
Vec3 operator-(const Vec3 &other_vec) const
Subtraction operator.
Definition algebra.hpp:442
friend Vec3 cross(const Vec3 v1, const Vec3 v2)
Friend function to calculate the cross product of two vectors.
Definition algebra.hpp:492
Vec3 operator*(const Vec3 &other_vec) const
Multiplication operator.
Definition algebra.hpp:406
T y
Definition algebra.hpp:282
Vec3 operator*(T scalar) const
Scalar multiplication (Vec3 * scalar).
Definition algebra.hpp:460
T sum() const
Calculate the sum of the vector components.
Definition algebra.hpp:548
Vec3 operator+(const Vec3 &other_vec) const
Addition operator.
Definition algebra.hpp:424
T z
The x, y, and z components of the vector.
Definition algebra.hpp:282
Vec3(T x, T y, T z)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:301
void normalize()
Normalize the vector to have a magnitude of 1.
Definition algebra.hpp:532
Vec3(const std::vector< T > &vec)
Constructs a Vec3 object from a std::vector.
Definition algebra.hpp:321
Vec3()
Default constructor initializing the vector to (0, 0, 0).
Definition algebra.hpp:289
T magnitude() const
Calculate the magnitude (length) of the vector.
Definition algebra.hpp:521
Vec3 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:370
friend Vec3 operator*(T scalar, const Vec3 &vec)
Scalar multiplication (scalar * Vec3).
Definition algebra.hpp:477
T x
Definition algebra.hpp:282
friend float dot(const Vec3 v1, const Vec3 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:511
bool operator!=(const Vec3 &other_vec) const
Inequality operator.
Definition algebra.hpp:355
Vec3 operator/(const Vec3 &other_vec) const
Division operator.
Definition algebra.hpp:387
Vec4 class for basic manipulation of 4D vectors.
Definition algebra.hpp:564
Vec4< T > adjust(float da, float db, float dc, float dd)
Adjusts the components of the vector by the given offsets.
Definition algebra.hpp:786
Vec4(const std::vector< T > &vec)
Constructs a Vec4 object from a std::vector.
Definition algebra.hpp:607
Vec4 & operator/=(const T value)
Division-assignment operator.
Definition algebra.hpp:656
bool operator==(const Vec4 &other_vec) const
Equality operator.
Definition algebra.hpp:627
friend Vec4 operator*(T scalar, const Vec4 &vec)
Scalar multiplication (scalar * Vec4).
Definition algebra.hpp:768
friend float dot(const Vec4 v1, const Vec4 v2)
Friend function to calculate the dot product of two vectors.
Definition algebra.hpp:801
Vec4 operator*(const Vec4 &other_vec) const
Multiplication operator.
Definition algebra.hpp:694
Vec4 operator+(const Vec4 &other_vec) const
Addition operator.
Definition algebra.hpp:713
T b
Definition algebra.hpp:565
Vec4 operator-(const Vec4 &other_vec) const
Subtraction operator.
Definition algebra.hpp:732
bool operator!=(const Vec4 &other_vec) const
Inequality operator.
Definition algebra.hpp:641
T d
The a, b, c, and d components of the vector.
Definition algebra.hpp:565
T a
Definition algebra.hpp:565
Vec4 operator*(T scalar) const
Scalar multiplication (Vec4 * scalar).
Definition algebra.hpp:751
T c
Definition algebra.hpp:565
Vec4 operator/(const Vec4 &other_vec) const
Division operator.
Definition algebra.hpp:674
Vec4(T a, T b, T c, T d)
Parameterized constructor initializing the vector to given values.
Definition algebra.hpp:586
Vec4()
Default constructor initializing the vector to (0, 0, 0, 0).
Definition algebra.hpp:573