Potato Engine
Loading...
Searching...
No Matches
Vector2.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <string>
5#include <cmath>
6
10struct Vector2
11{
13 float x;
15 float y;
16
18 constexpr Vector2() : x{0.f}, y{0.f} {}
20 constexpr Vector2(float x, float y) : x(x), y(y) {}
22 constexpr Vector2(const Vector2& vec) : x(vec.x), y(vec.y) {}
23
25 inline Vector2 operator +(const Vector2& other) const {
26 return Vector2(x + other.x, y + other.y);
27 }
28
29 inline void operator +=(const Vector2& other) {
30 x += other.x;
31 y += other.y;
32 }
33
34 inline Vector2 operator -(const Vector2& other) const {
35 return Vector2(x - other.x, y - other.y);
36 }
37
38 inline void operator -=(const Vector2& other) {
39 x -= other.x;
40 y -= other.y;
41 }
42
43 inline Vector2 operator *(float scalar) const {
44 return Vector2(x * scalar, y * scalar);
45 }
46
47 inline Vector2 operator *(const Vector2& other) const {
48 return Vector2(x*other.x, y*other.y);
49 }
50
51 inline Vector2 operator / (float scalar) const {
52 return Vector2(x / scalar, y / scalar);
53 }
54
55 inline Vector2 operator -() const {
56 return Vector2(-x, -y);
57 }
58
60 inline Vector2 Swizzled() const {
61 return Vector2(y, x);
62 }
63
64 inline float Dot(const Vector2& other) const {
65 return x * other.x + y * other.y;
66 }
67
68 inline float Magnitude() const {
69 return std::sqrt(x * x + y * y);
70 }
71
72 inline Vector2 Normalized() const {
73 float mag = Magnitude();
74 if (mag == 0.f) return Vector2(0.f, 0.f);
75 return Vector2(x / mag, y / mag);
76 }
77
79 inline std::string ToString() const {
80 return "(" + std::to_string((int)x) + ", " + std::to_string((int)y) + ")";
81 }
82
83};
84
85inline Vector2 operator *(float scalar, const Vector2& vec) {
86 return vec * scalar;
87}
Standard 2-dimensional vector.
Definition Vector2.hpp:11
float x
X Component.
Definition Vector2.hpp:13
void operator+=(const Vector2 &other)
Adds to vector.
Definition Vector2.hpp:29
float y
Y Component.
Definition Vector2.hpp:15
float Magnitude() const
Definition Vector2.hpp:68
float Dot(const Vector2 &other) const
Definition Vector2.hpp:64
Vector2 Normalized() const
Definition Vector2.hpp:72
Vector2 operator*(float scalar) const
Definition Vector2.hpp:43
Vector2 operator/(float scalar) const
Definition Vector2.hpp:51
Vector2 operator+(const Vector2 &other) const
Definition Vector2.hpp:25
constexpr Vector2(float x, float y)
Constructs custom vector.
Definition Vector2.hpp:20
Vector2 Swizzled() const
Definition Vector2.hpp:60
Vector2 operator-() const
Definition Vector2.hpp:55
constexpr Vector2()
Constructs zero vector.
Definition Vector2.hpp:18
std::string ToString() const
Definition Vector2.hpp:79
void operator-=(const Vector2 &other)
Subtracts from vector.
Definition Vector2.hpp:38
constexpr Vector2(const Vector2 &vec)
Constructs vector from copy.
Definition Vector2.hpp:22