Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
vector_2.h
1#ifndef __VEC2_H__
2#define __VEC2_H__ 1
3
4#include <math.h>
5namespace coma {
7class Vec2 {
8 public:
9 Vec2();
10 Vec2(float x, float y);
11 Vec2(const Vec2& copy);
12 ~Vec2();
13
14 Vec2 operator+(const Vec2& other) const;
15 Vec2 operator+(float value);
16 Vec2& operator+=(const Vec2& other);
17 Vec2& operator+=(float value);
18 Vec2 operator-(const Vec2& other) const;
19 Vec2 operator-(float value) const;
20 Vec2 operator-() const;
21 Vec2& operator-=(const Vec2& other);
22 Vec2& operator-=(float value);
23 bool operator==(const Vec2& other) const;
24 bool operator!=(const Vec2& other) const;
25 void operator=(const Vec2& other);
26 void operator=(float value);
27 Vec2 operator*(float value) const;
28 Vec2& operator*=(float value);
29 Vec2 operator/(float value) const;
30 Vec2& operator/=(float value);
33 float Magnitude() const;
34
36 void Normalize();
37
40 Vec2 Normalized() const;
41
44 void Scale(const Vec2 scale);
45
48 float SqrMagnitude() const;
49
54 static float Distance(const Vec2 a, const Vec2 b);
55
60 static float DotProduct(Vec2 a, Vec2 b);
61
67 static Vec2 Lerp(const Vec2 a, const Vec2 b, float t);
68
74 static Vec2 LerpUnclamped(const Vec2 a, const Vec2 b, float t);
75
79 static float Radians(const Vec2 v);
80
81 static const Vec2 up;
82 static const Vec2 down;
83 static const Vec2 right;
84 static const Vec2 left;
85 static const Vec2 zero;
86 static const Vec2 one;
87
88 float x;
89 float y;
90};
91
92inline Vec2 Vec2::operator+(const Vec2& other) const {
93 return Vec2(x + other.x, y + other.y);
94}
95
96inline Vec2 Vec2::operator+(float value) { return Vec2(x + value, y + value); }
97
98inline Vec2& Vec2::operator+=(const Vec2& other) {
99 x += other.x;
100 y += other.y;
101 return *this;
102}
103
104inline Vec2& Vec2::operator+=(float value) {
105 x += value;
106 y += value;
107 return *this;
108}
109
110inline Vec2 Vec2::operator-(const Vec2& other) const {
111 return Vec2(x - other.x, y - other.y);
112}
113
114inline Vec2 Vec2::operator-(float value) const {
115 return Vec2(x - value, y - value);
116}
117
118inline Vec2 Vec2::operator-() const { return Vec2(-x, -y); }
119
120inline Vec2& Vec2::operator-=(const Vec2& other) {
121 x -= other.x;
122 y -= other.y;
123 return *this;
124}
125
126inline Vec2& Vec2::operator-=(float value) {
127 x -= value;
128 y -= value;
129 return *this;
130}
131
132inline bool Vec2::operator==(const Vec2& value) const {
133 if (x == value.x && y == value.y) return true;
134 return false;
135}
136
137inline bool Vec2::operator!=(const Vec2& value) const {
138 if (x == value.x && y == value.y) return false;
139 return true;
140}
141
142inline void Vec2::operator=(const Vec2& other) {
143 x = other.x;
144 y = other.y;
145}
146
147inline void Vec2::operator=(float value) {
148 x = value;
149 y = value;
150}
151
152inline Vec2 Vec2::operator*(float value) const {
153 return Vec2(x * value, y * value);
154}
155
156inline Vec2& Vec2::operator*=(float value) {
157 x *= value;
158 y *= value;
159 return *this;
160}
161
162inline Vec2 Vec2::operator/(float value) const {
163 return Vec2(x / value, y / value);
164}
165
166inline Vec2& Vec2::operator/=(float value) {
167 x /= value;
168 y /= value;
169 return *this;
170}
171
172inline float Vec2::Magnitude() const { return sqrtf(x * x + y * y); }
173
174inline void Vec2::Normalize() {
175 if (Magnitude() != 0) *this /= Magnitude();
176}
177
178inline Vec2 Vec2::Normalized() const {
179 float mag = Magnitude();
180 if (mag == 0) return Vec2(0, 0);
181 return Vec2(x / mag, y / mag);
182}
183
184inline void Vec2::Scale(const Vec2 scale) {
185 x *= scale.x;
186 y *= scale.y;
187}
188
189inline float Vec2::SqrMagnitude() const { return x * x + y * y; }
190
191inline float Vec2::Distance(const Vec2 a, const Vec2 b) {
192 return (b - a).Magnitude();
193}
194
195inline Vec2 Vec2::Lerp(const Vec2 a, const Vec2 b, float t) {
196 t = t > 1 ? 1 : t;
197 t = t < 0 ? 0 : t;
198 return (b - a) * t + a;
199}
200
201inline Vec2 Vec2::LerpUnclamped(const Vec2 a, const Vec2 b, float t) {
202 return (b - a) * t + a;
203}
204
205inline float Vec2::Radians(const Vec2 v) { return atan2f(v.y, v.x); }
206
207inline float Vec2::DotProduct(Vec2 a, Vec2 b) { return a.x * b.x + a.y * b.y; }
208} // namespace coma
209#endif
represents mathematical vector with 2 components
Definition: vector_2.h:7
float Magnitude() const
Definition: vector_2.h:172
static float Radians(const Vec2 v)
Definition: vector_2.h:205
void Scale(const Vec2 scale)
Definition: vector_2.h:184
static float DotProduct(Vec2 a, Vec2 b)
Definition: vector_2.h:207
float SqrMagnitude() const
Definition: vector_2.h:189
static float Distance(const Vec2 a, const Vec2 b)
Definition: vector_2.h:191
Vec2 Normalized() const
Definition: vector_2.h:178
static Vec2 LerpUnclamped(const Vec2 a, const Vec2 b, float t)
Definition: vector_2.h:201
static Vec2 Lerp(const Vec2 a, const Vec2 b, float t)
Definition: vector_2.h:195
copperdielf Math Library
Definition: buffer.hpp:5