-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.h
221 lines (184 loc) · 3.33 KB
/
Math.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#pragma once
#include <cmath>
#include <memory.h>
#include <limits>
namespace Math
{
const float PI = 3.141592f;
const float Infinity = std::numeric_limits<float>::infinity();
const float NegInfinity = -std::numeric_limits<float>::infinity();
/*度数法 -> 弧度法*/
inline float ToRadians(float degrees)
{
return degrees * PI / 180.0f;
}
/*弧度法 -> 度数法*/
inline float ToDegrees(float radians)
{
return radians * 180.0f / PI;
}
inline bool NearZero(float val, float epsilon = 0.001f)
{
if (fabs(val) <= epsilon)
{
return true;
}
else
{
return false;
}
}
template<typename T>
T Max(const T& a, const T& b)
{
return (a < b ? b : a);
}
template<typename T>
T Min(const T& a, const T& b)
{
return (a < b ? a : b);
}
template<typename T>
T Clamp(const T& val, const T& lower, const T& upper)
{
return Min(upper, Max(lower, val));
}
inline float Abs(float val)
{
return fabs(val);
}
inline float Cos(float angle)
{
return cosf(angle);
}
inline float Sin(float angle)
{
return sinf(angle);
}
inline float Tan(float angle)
{
return tanf(angle);
}
inline float Acos(float angle)
{
return acosf(angle);
}
inline float Asin(float angle)
{
return asinf(angle);
}
inline float Atan(float angle)
{
return atanf(angle);
}
/*tanの逆数*/
inline float Cot(float angle)
{
return 1.0f / Tan(angle);
}
inline float Lerp(float a, float b, float t)
{
return a + (b - a) * t;
}
inline float Sqrt(float val)
{
return sqrtf(val);
}
}
class Vector2
{
public:
float x;
float y;
Vector2()
:x(0.0f)
,y(0.0f)
{}
explicit Vector2(float inX, float inY)
:x(inX)
,y(inY)
{}
friend Vector2 operator+(const Vector2& a, const Vector2& b)
{
return Vector2(a.x + b.x, a.y + b.y);
}
friend Vector2 operator-(const Vector2& a, const Vector2& b)
{
return Vector2(a.x - b.x, a.y - b.y);
}
friend Vector2 operator*(const Vector2& a, const Vector2& b)
{
return Vector2(a.x * b.x, a.y * b.y);
}
friend Vector2 operator*(const Vector2& a, float scalar)
{
return Vector2(a.x * scalar, a.y * scalar);
}
friend Vector2 operator/(const Vector2& a, float divide)
{
return Vector2(a.x / divide, a.y / divide);
}
Vector2& operator+=(const Vector2& add)
{
x += add.x;
y += add.y;
return *this;
}
Vector2& operator-=(const Vector2& subtract)
{
x -= subtract.x;
y -= subtract.y;
return *this;
}
Vector2& operator*=(float scalar)
{
x *= scalar;
y *= scalar;
return *this;
}
Vector2& operator/=(float divide)
{
x /= divide;
y /= divide;
return *this;
}
/*ベクトルの大きさの2乗*/
float LengthSq() const
{
return (x * x + y * y);
}
/*ベクトルの大きさ*/
float Length()
{
return (Math::Sqrt(LengthSq()));
}
/*ベクトルを正規化する*/
void Normalize()
{
float length = Length();
x /= length;
y /= length;
}
/*与えられたベクトルを正規化した値を返す*/
static Vector2 Normalize(const Vector2& a)
{
Vector2 temp = a;
temp.Normalize();
return temp;
}
/*線形補間*/
static Vector2 Lerp(const Vector2& a, const Vector2& b, float t)
{
return Vector2(a + (b - a) * t);
}
/*内積*/
static float Dot(const Vector2& a, const Vector2& b)
{
return (a.x * b.x + a.y * b.y);
}
static const Vector2 Zero;
static const Vector2 UnitX;
static const Vector2 UnitY;
static const Vector2 NegUnitX;
static const Vector2 NegUnitY;
};