MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_vector2.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_VECTOR2_H_1606631290
12#define INCGUARD_MGL_VECTOR2_H_1606631290
13
14#include <algorithm>
15#include <cmath>
16
18
19namespace MGL
20{
22struct Vector2
23{
24 float x;
25 float y;
26
27 /* ------------------------------------------------------------------------- */
31 /* ------------------------------------------------------------------------- */
32 constexpr Vector2() noexcept
33 : x(0.0f)
34 , y(0.0f)
35 {}
36
37 /* ------------------------------------------------------------------------- */
43 /* ------------------------------------------------------------------------- */
44 constexpr Vector2(float inX, float inY) noexcept
45 : x(inX)
46 , y(inY)
47 {}
48
49 /* ------------------------------------------------------------------------- */
53 /* ------------------------------------------------------------------------- */
54 constexpr Vector2 &operator+=(const Vector2 &rhs) noexcept
55 {
56 this->x += rhs.x;
57 this->y += rhs.y;
58
59 return *this;
60 }
61
62 /* ------------------------------------------------------------------------- */
66 /* ------------------------------------------------------------------------- */
67 constexpr Vector2 &operator-=(const Vector2 &rhs) noexcept
68 {
69 this->x -= rhs.x;
70 this->y -= rhs.y;
71
72 return *this;
73 }
74
75 /* ------------------------------------------------------------------------- */
79 /* ------------------------------------------------------------------------- */
80 constexpr Vector2 &operator*=(float rhs) noexcept
81 {
82 this->x *= rhs;
83 this->y *= rhs;
84
85 return *this;
86 }
87
88 /* ------------------------------------------------------------------------- */
92 /* ------------------------------------------------------------------------- */
93 constexpr Vector2 &operator/=(float rhs) noexcept
94 {
95 this->x /= rhs;
96 this->y /= rhs;
97
98 return *this;
99 }
100
101 /* ------------------------------------------------------------------------- */
107 /* ------------------------------------------------------------------------- */
108 [[nodiscard]] bool IsZero() const noexcept
109 {
110 return ((std::fabs(x) <= Math::kEpsilon) && (std::fabs(y) <= Math::kEpsilon));
111 }
112
113 /* ------------------------------------------------------------------------- */
118 /* ------------------------------------------------------------------------- */
119 [[nodiscard]] float Length() const noexcept
120 {
121 return std::sqrt((x * x) + (y * y));
122 }
123
124 /* ------------------------------------------------------------------------- */
130 /* ------------------------------------------------------------------------- */
131 [[nodiscard]] float Distance(const Vector2 &target) const noexcept
132 {
133 return Vector2(x - target.x, y - target.y).Length();
134 }
135
136 /* ------------------------------------------------------------------------- */
142 /* ------------------------------------------------------------------------- */
143 [[nodiscard]] float Cross(const Vector2 &rhs) const noexcept
144 {
145 return (x * rhs.y) - (rhs.x * y);
146 }
147
148 /* ------------------------------------------------------------------------- */
155 /* ------------------------------------------------------------------------- */
156 [[nodiscard]] static float Cross(const Vector2 &lhs, const Vector2 &rhs) noexcept
157 {
158 return (lhs.x * rhs.y) - (rhs.x * lhs.y);
159 }
160
161 /* ------------------------------------------------------------------------- */
167 /* ------------------------------------------------------------------------- */
168 [[nodiscard]] float Dot(const Vector2 &rhs) const noexcept
169 {
170 return (x * rhs.x) + (y * rhs.y);
171 }
172
173 /* ------------------------------------------------------------------------- */
180 /* ------------------------------------------------------------------------- */
181 [[nodiscard]] static float Dot(const Vector2 &lhs, const Vector2 &rhs) noexcept
182 {
183 return (lhs.x * rhs.x) + (lhs.y * rhs.y);
184 }
185
186 /* ------------------------------------------------------------------------- */
192 /* ------------------------------------------------------------------------- */
193 [[nodiscard]] float GetAngle(const Vector2 &target) const noexcept
194 {
195 auto length = Length() * target.Length();
196 if ((length == 0.0f) || (length == -0.0f))
197 {
198 return 0.0f;
199 }
200
201 auto dot = Dot(target);
202 if ((dot == 0.0f) || (dot == -0.0f))
203 {
204 return Math::kHalfPi;
205 }
206
207 const float theta = std::clamp(dot / length, -1.0f, 1.0f);
208 return std::acos(theta);
209 }
210
211 /* ------------------------------------------------------------------------- */
217 /* ------------------------------------------------------------------------- */
218 bool Normalize() noexcept
219 {
220 if ((x == 0.0f) || (x == -0.0f))
221 {
222 if ((y == 0.0f) || (y == -0.0f))
223 {
224 return false;
225 }
226 }
227
228 auto length = Length();
229 x /= length;
230 y /= length;
231
232 return true;
233 }
234
235 /* ------------------------------------------------------------------------- */
240 /* ------------------------------------------------------------------------- */
241 void Rotate(float radian) noexcept
242 {
243 auto srcX = x;
244 x = srcX * std::cos(radian) - y * std::sin(radian);
245 y = srcX * std::sin(radian) + y * std::cos(radian);
246 }
247};
248
249/* ------------------------------------------------------------------------- */
256/* ------------------------------------------------------------------------- */
257constexpr Vector2 operator+(const Vector2 &lhs, const Vector2 &rhs) noexcept
258{
259 return {lhs.x + rhs.x, lhs.y + rhs.y};
260}
261
262/* ------------------------------------------------------------------------- */
269/* ------------------------------------------------------------------------- */
270constexpr Vector2 operator-(const Vector2 &lhs, const Vector2 &rhs) noexcept
271{
272 return {lhs.x - rhs.x, lhs.y - rhs.y};
273}
274
275/* ------------------------------------------------------------------------- */
282/* ------------------------------------------------------------------------- */
283constexpr Vector2 operator*(const Vector2 &lhs, float rhs) noexcept
284{
285 return {lhs.x * rhs, lhs.y * rhs};
286}
287
288/* ------------------------------------------------------------------------- */
295/* ------------------------------------------------------------------------- */
296constexpr Vector2 operator/(const Vector2 &lhs, float rhs) noexcept
297{
298 return {lhs.x / rhs, lhs.y / rhs};
299}
300} // namespace MGL
301
302#endif // INCGUARD_MGL_VECTOR2_H_1606631290
303
304// vim: et ts=4 sw=4 sts=4
MGL 数学系ユーティリティ
constexpr Vector2 operator-(const Vector2 &lhs, const Vector2 &rhs) noexcept
Vector2の減算
Definition mgl_vector2.h:270
constexpr Vector2 operator*(const Vector2 &lhs, float rhs) noexcept
Vector2の乗算
Definition mgl_vector2.h:283
constexpr Vector2 operator/(const Vector2 &lhs, float rhs) noexcept
Vector2の除算
Definition mgl_vector2.h:296
constexpr Vector2 operator+(const Vector2 &lhs, const Vector2 &rhs) noexcept
Vector2の加算
Definition mgl_vector2.h:257
2Dベクトル
Definition mgl_vector2.h:23
float y
Y成分
Definition mgl_vector2.h:25
float Cross(const Vector2 &rhs) const noexcept
外積を求める
Definition mgl_vector2.h:143
static float Cross(const Vector2 &lhs, const Vector2 &rhs) noexcept
外積を求める
Definition mgl_vector2.h:156
static float Dot(const Vector2 &lhs, const Vector2 &rhs) noexcept
内積を求める
Definition mgl_vector2.h:181
bool IsZero() const noexcept
値がほぼゼロかを調べる
Definition mgl_vector2.h:108
constexpr Vector2 & operator+=(const Vector2 &rhs) noexcept
単項加算演算子
Definition mgl_vector2.h:54
constexpr Vector2(float inX, float inY) noexcept
値を指定して初期化
Definition mgl_vector2.h:44
float Distance(const Vector2 &target) const noexcept
ターゲットまでの距離を求める
Definition mgl_vector2.h:131
constexpr Vector2 & operator-=(const Vector2 &rhs) noexcept
単項減算演算子
Definition mgl_vector2.h:67
float Dot(const Vector2 &rhs) const noexcept
内積を求める
Definition mgl_vector2.h:168
float GetAngle(const Vector2 &target) const noexcept
2点間の角度を求める
Definition mgl_vector2.h:193
float Length() const noexcept
長さを求める
Definition mgl_vector2.h:119
void Rotate(float radian) noexcept
ベクトルを回転
Definition mgl_vector2.h:241
bool Normalize() noexcept
ベクトルを正規化する
Definition mgl_vector2.h:218
constexpr Vector2 & operator/=(float rhs) noexcept
単項除算演算子
Definition mgl_vector2.h:93
constexpr Vector2 & operator*=(float rhs) noexcept
単項乗算演算子
Definition mgl_vector2.h:80
constexpr Vector2() noexcept
ゼロ初期化
Definition mgl_vector2.h:32
float x
X成分
Definition mgl_vector2.h:24