system
The vocabulary types everything else shares
Loading...
Searching...
No Matches
Vector2.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <cmath>
14#include <cstdint>
15#include <type_traits>
16
17#include "Constants.hpp"
18
19template <typename T>
20struct Vector2 {
21 T x{}, y{};
22
23 // Comparison Operators
25 bool operator==(const Vector2& other) const { return x == other.x && y == other.y; }
26
28 bool operator!=(const Vector2& other) const { return !(*this == other); }
29
30 // Arithmetic Operators
32 Vector2 operator+(const Vector2& other) const { return Vector2{x + other.x, y + other.y}; }
33
35 Vector2 operator-(const Vector2& other) const { return Vector2{x - other.x, y - other.y}; }
36
38 Vector2 operator*(T scalar) const { return Vector2{x * scalar, y * scalar}; }
39
41 Vector2 operator/(T scalar) const { return Vector2{x / scalar, y / scalar}; }
42
43 // Compound Assignment Operators
45 Vector2& operator+=(const Vector2& other) { x += other.x; y += other.y; return *this; }
46
48 Vector2& operator-=(const Vector2& other) { x -= other.x; y -= other.y; return *this; }
49
51 Vector2& operator*=(T scalar) { x *= scalar; y *= scalar; return *this; }
52
54 Vector2& operator/=(T scalar) { x /= scalar; y /= scalar; return *this; }
55
56 // Unary Operators
58 Vector2 operator-() const { return Vector2{-x, -y}; }
59
60 //methods
62 T dot(const Vector2& other) const { return x * other.x + y * other.y; }
63
65 T cross(const Vector2& other) const { return x * other.y - y * other.x; }
66
68 T square_magnitude() const { return x*x + y*y; }
69
71 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
72 magnitude() const { return std::sqrt(square_magnitude()); }
73
75 typename std::conditional<std::is_floating_point<T>::value, T, double>::type
76 distance(const Vector2& other) const { return (*this - other).magnitude(); }
77
79 double angle(const Vector2& other) const {
80 double dot = double(this->dot(other));
81 double cross = double(this->cross(other));
82
83 return std::atan2(cross, dot); // result in radians, can be negative
84 }
85
88 T len = magnitude();
89 if (len == 0) return {0, 0};
90 return {T(x / len), T(y / len)};
91 }
92
94 void normalize() {
95 double len = magnitude();
96
97 if (len == 0) return;
98 x /= len;
99 y /= len;
100 }
101
103 bool same(const Vector2& other, T epsilon = epsilon_v<T>) const {
104 return std::abs(x - other.x) < epsilon &&
105 std::abs(y - other.y) < epsilon;
106 };
107};
108
109// ---------- Aliases ----------
113
114
constexpr T epsilon_v
Definition Constants.hpp:32
Definition Vector2.hpp:20
Vector2 operator/(T scalar) const
Definition Vector2.hpp:41
void normalize()
Definition Vector2.hpp:94
Vector2 & operator+=(const Vector2 &other)
Definition Vector2.hpp:45
Vector2 & operator*=(T scalar)
Definition Vector2.hpp:51
T y
Definition Vector2.hpp:21
bool same(const Vector2 &other, T epsilon=epsilon_v< T >) const
Definition Vector2.hpp:103
T x
Definition Vector2.hpp:21
Vector2 & operator-=(const Vector2 &other)
Definition Vector2.hpp:48
T cross(const Vector2 &other) const
Definition Vector2.hpp:65
Vector2 operator-(const Vector2 &other) const
Definition Vector2.hpp:35
Vector2 operator+(const Vector2 &other) const
Definition Vector2.hpp:32
Vector2 operator-() const
Definition Vector2.hpp:58
T square_magnitude() const
Definition Vector2.hpp:68
std::conditional< std::is_floating_point< T >::value, T, double >::type magnitude() const
Definition Vector2.hpp:72
std::conditional< std::is_floating_point< T >::value, T, double >::type distance(const Vector2 &other) const
Definition Vector2.hpp:76
Vector2 normalized() const
Definition Vector2.hpp:87
Vector2 & operator/=(T scalar)
Definition Vector2.hpp:54
T dot(const Vector2 &other) const
Definition Vector2.hpp:62
double angle(const Vector2 &other) const
Definition Vector2.hpp:79
Vector2 operator*(T scalar) const
Definition Vector2.hpp:38