-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathHelper.h
175 lines (140 loc) · 3.23 KB
/
MathHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef _MATHHELPER_H
#define _MATHHELPER_H
#include <math.h>
constexpr auto PI = 3.14159265;
constexpr auto DEG_TO_RAD = PI / 180.0f;
constexpr auto RAD_TO_DEG = 180.0f / PI;
struct Vector2
{
float x;
float y;
Vector2(float _x = 0.0f, float _y = 0.0f)
:x(_x), y(_y){}
float MagnitudeSqr()
{
return x * x + y * y;
}
float Magnitude()
{
return (float)sqrt(x*x + y * y);
}
Vector2 Normalized()
{
float mag = Magnitude();
return Vector2(x / mag, y / mag);
}
Vector2& operator +=(const Vector2& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2& operator -=(const Vector2& rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
Vector2 operator -() const
{
return Vector2(-x, -y);
}
};
inline Vector2 operator +(const Vector2& lhs, const Vector2& rhs)
{
return Vector2(lhs.x + rhs.x, lhs.y + rhs.y);
}
inline Vector2 operator -(const Vector2& lhs, const Vector2& rhs)
{
return Vector2(lhs.x - rhs.x, lhs.y - rhs.y);
}
inline Vector2 operator *(const Vector2& lhs, const float& rhs)
{
return Vector2(lhs.x * rhs, lhs.y * rhs);
}
inline Vector2 operator *(const float& lhs, const Vector2& rhs)
{
return Vector2(lhs * rhs.x, lhs * rhs.y);
}
inline Vector2 operator *(const Vector2& lhs, const Vector2& rhs)
{
return Vector2(lhs.x * rhs.x, lhs.y * rhs.y);
}
inline Vector2 operator /(const Vector2& lhs, const float& rhs)
{
return Vector2(lhs.x / rhs, lhs.y / rhs);
}
inline Vector2 operator /(const float& lhs, const Vector2& rhs)
{
return Vector2(lhs / rhs.x, lhs / rhs.y);
}
inline bool operator <(const Vector2& lhs, const Vector2& rhs)
{
return lhs.x < rhs.x && lhs.y < rhs.y;
}
inline bool operator >(const Vector2& lhs, const Vector2& rhs)
{
return lhs.x > rhs.x && lhs.y > rhs.y;
}
inline bool operator !=(const Vector2& lhs, const Vector2& rhs)
{
return lhs.x != rhs.x && lhs.y != rhs.y;
}
inline bool operator ==(const Vector2& lhs, const Vector2& rhs)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
inline Vector2 RotateVector(Vector2& vec, float angle)
{
float radAngle = (float)(angle*(DEG_TO_RAD));
return Vector2((float)(vec.x * cos(radAngle) - vec.y * sin(radAngle)), (float) (vec.x * sin(radAngle) + vec.y * cos (radAngle)));
}
inline float Dot(const Vector2& vec1, const Vector2& vec2)
{
return vec1.x * vec2.x + vec1.y * vec2.y;
}
inline float Clamp(const float& value, const float& min, const float& max)
{
if (value > max)
{
return max;
} else if (value < min)
{
return min;
}
return value;
}
inline Vector2 Lerp(Vector2& start, Vector2& end, float time)
{
if (time <= 0.0f)
return start;
if (time >= 1.0f)
return end;
Vector2 dir = (end - start).Normalized();
float mag = (end - start).Magnitude();
return start + dir * mag * time;
}
const Vector2 VEC2_ZERO = { 0.0f, 0.0f };
const Vector2 VEC2_ONE = { 1.0f, 1.0f };
const Vector2 VEC2_UP = { 0.0f, 1.0f };
const Vector2 VEC2_RIGHT = { 1.0f, 0.0f };
struct BezierCurve
{
Vector2 p0;
Vector2 p1;
Vector2 p2;
Vector2 p3;
Vector2 CalculateCurvePoint(float t)
{
float tt = t * t;
float ttt = tt * t;
float u = 1.0f - t;
float uu = u * u;
float uuu = uu * u;
Vector2 point = (uuu * p0) + (3 * uu * t * p1) + (3 * u * tt * p2) + (ttt * p3);
point.x = round(point.x);
point.y = round(point.y);
return point;
}
};
#endif