UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
Vector2D.hpp
[詳解]
1//
2// 二次元ベクトル 前方定義
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
11
12namespace Udon
13{
14
15 struct Polar;
16
17 struct Vec3;
18
19
21 struct Vec2
22 {
23
25 using ValueType = double;
26
29
32
34 static constexpr size_t Dimension = 2;
35
37 constexpr Vec2() noexcept
38 : x()
39 , y()
40 {
41 }
42
46 constexpr Vec2(ValueType x, ValueType y) noexcept
47 : x(x)
48 , y(y)
49 {
50 }
51
53 Vec2(const Vec2&) = default;
54
56 Vec2& operator=(const Vec2&) = default;
57
58 friend constexpr Vec2 operator+(const Vec2& lhs, const Vec2& rhs) noexcept { return { lhs.x + rhs.x, lhs.y + rhs.y }; }
59 friend constexpr Vec2 operator-(const Vec2& lhs, const Vec2& rhs) noexcept { return { lhs.x - rhs.x, lhs.y - rhs.y }; }
60 friend constexpr Vec2 operator*(const Vec2& lhs, const Vec2& rhs) noexcept { return { lhs.x * rhs.x, lhs.y * rhs.y }; }
61 friend constexpr Vec2 operator/(const Vec2& lhs, const Vec2& rhs) noexcept { return { lhs.x / rhs.x, lhs.y / rhs.y }; }
62
63 friend constexpr Vec2 operator+(const Vec2& lhs, ValueType rhs) noexcept { return { lhs.x + rhs, lhs.y + rhs }; }
64 friend constexpr Vec2 operator-(const Vec2& lhs, ValueType rhs) noexcept { return { lhs.x - rhs, lhs.y - rhs }; }
65 friend constexpr Vec2 operator*(const Vec2& lhs, ValueType rhs) noexcept { return { lhs.x * rhs, lhs.y * rhs }; }
66 friend constexpr Vec2 operator/(const Vec2& lhs, ValueType rhs) noexcept { return { lhs.x / rhs, lhs.y / rhs }; }
67
68 friend constexpr Vec2 operator+(ValueType lhs, const Vec2& rhs) noexcept { return { lhs + rhs.x, lhs + rhs.x }; }
69 friend constexpr Vec2 operator-(ValueType lhs, const Vec2& rhs) noexcept { return { lhs - rhs.x, lhs - rhs.x }; }
70 friend constexpr Vec2 operator*(ValueType lhs, const Vec2& rhs) noexcept { return { lhs * rhs.x, lhs * rhs.x }; }
71 friend constexpr Vec2 operator/(ValueType lhs, const Vec2& rhs) noexcept { return { lhs / rhs.x, lhs / rhs.x }; }
72
73 Vec2& operator+=(const Vec2& rhs) noexcept { return *this = *this + rhs; };
74 Vec2& operator-=(const Vec2& rhs) noexcept { return *this = *this - rhs; };
75 Vec2& operator*=(const Vec2& rhs) noexcept { return *this = *this * rhs; };
76 Vec2& operator/=(const Vec2& rhs) noexcept { return *this = *this / rhs; };
77
78 Vec2& operator+=(ValueType rhs) noexcept { return *this = *this + rhs; };
79 Vec2& operator-=(ValueType rhs) noexcept { return *this = *this - rhs; };
80 Vec2& operator*=(ValueType rhs) noexcept { return *this = *this * rhs; };
81 Vec2& operator/=(ValueType rhs) noexcept { return *this = *this / rhs; };
82
83 friend constexpr bool operator==(const Vec2& lhs, const Vec2& rhs) noexcept { return lhs.x == rhs.x and lhs.y == rhs.y; };
84 friend constexpr bool operator!=(const Vec2& lhs, const Vec2& rhs) noexcept { return not(lhs == rhs); };
85
88 explicit constexpr operator bool() const noexcept
89 {
90 return x or y;
91 }
92
95 static constexpr Vec2 Zero() noexcept
96 {
97 return { 0, 0 };
98 }
99
101 constexpr bool isZero() const noexcept
102 {
103 return not operator bool();
104 }
105
107 void clear() noexcept
108 {
109 *this = Zero();
110 }
111
112 Vec2& replaceX(ValueType newValue) noexcept
113 {
114 x = newValue;
115 return *this;
116 }
117
118 Vec2& replaceY(ValueType newValue) noexcept
119 {
120 y = newValue;
121 return *this;
122 }
123
124 template <typename Visitor>
125 Vec2& replaceX(Visitor&& visitor) noexcept
126 {
127 x = visitor(x);
128 return *this;
129 }
130
131 template <typename Visitor>
132 Vec2& replaceY(Visitor&& visitor) noexcept
133 {
134 y = visitor(y);
135 return *this;
136 }
137
140 Vec2 abs() const noexcept
141 {
142 return {
143 Udon::Abs(x),
144 Udon::Abs(y)
145 };
146 }
147
152 Vec2 rotatedAt(const Vec2& center, ValueType angle) const noexcept
153 {
154 const auto s = sin(-angle);
155 const auto c = cos(-angle);
156 const auto d = (*this - center);
157 return center + Vec2{ (d.x * c - d.y * s), (d.x * s + d.y * c) };
158 }
159
164 Vec2& rotateAt(const Vec2& center, ValueType angle) noexcept
165 {
166 return *this = rotatedAt(center, angle);
167 }
168
172 Vec2 rotated(ValueType angle) const noexcept
173 {
174 return rotatedAt(Zero(), angle);
175 }
176
181 {
182 return *this = rotated(angle);
183 }
184
189 ValueType angleAt(const Vec2& rhs) const noexcept
190 {
191 if (*this)
192 {
193 const auto d = *this - rhs;
194 return atan2(d.x, d.y);
195 }
196 else
197 {
198 return 0.;
199 }
200 }
201
205 ValueType angle() const noexcept
206 {
207 return angleAt(Zero());
208 }
209
213 ValueType distanceFrom(const Vec2& rhs) const noexcept
214 {
215 const auto d = *this - rhs;
216 return sqrt(d.x * d.x + d.y * d.y);
217 }
218
221 ValueType length() const noexcept
222 {
223 return distanceFrom(Zero());
224 }
225
228 constexpr ValueType lengthSq() const noexcept
229 {
230 return x * x + y * y;
231 }
232
236 {
237 return normalized() * length;
238 }
239
244 {
245 return *this = scaledLength(length);
246 }
247
254 constexpr Vec2 mapped(ValueType fromMin, ValueType fromMax, ValueType toMin, ValueType toMax) const noexcept
255 {
256 return {
257 Map(x, fromMin, fromMax, toMin, toMax),
258 Map(y, fromMin, fromMax, toMin, toMax),
259 };
260 }
261
268 Vec2& map(ValueType fromMin, ValueType fromMax, ValueType toMin, ValueType toMax) noexcept
269 {
270 return *this = mapped(fromMin, fromMax, toMin, toMax);
271 }
272
275 Vec2 normalized() const noexcept
276 {
277 if (const auto len = length())
278 {
279 return *this / len;
280 }
281 else
282 {
283 return Zero();
284 }
285 }
286
289 Vec2& normalize() noexcept
290 {
291 return *this = normalized();
292 }
293
298 constexpr Vec2 clamped(ValueType min, ValueType max) const noexcept
299 {
300 return { Udon::Constrain(x, min, max), Udon::Constrain(y, min, max) };
301 }
302
307 Vec2& clamp(ValueType min, ValueType max) noexcept
308 {
309 return *this = clamped(min, max);
310 }
311
312 Udon::Vec3 xy0() const noexcept;
313
314 Udon::Polar toPolar() const noexcept;
315
316#ifdef SIV3D_INCLUDED
317
320 template <typename T>
321 constexpr Vec2(const s3d::Vector2D<T>& v) noexcept
322 : x(static_cast<ValueType>(v.x))
323 , y(static_cast<ValueType>(v.y))
324 {
325 }
326
328 template <typename T>
329 [[nodiscard]] s3d::Vector2D<T> asSivVec2() const noexcept
330 {
331 return {
332 static_cast<T>(x),
333 static_cast<T>(y),
334 };
335 }
336
338 template <typename T>
339 [[nodiscard]]
340 operator s3d::Vector2D<T>() const noexcept
341 {
342 return asSivVec2<T>();
343 }
344#endif
345
346#ifdef ARDUINO
348 void show() const
349 {
350 Serial.print(F("x: ")), Serial.print(x), Serial.print('\t');
351 Serial.print(F("y: ")), Serial.print(y), Serial.print('\t');
352 }
353#endif
354
356 };
357
358} // namespace Udon
#define F(x)
Definition Show.hpp:17
Definition Bit.hpp:12
constexpr T Abs(const T &rhs)
絶対値を返す (std::abs)
Definition Math.hpp:98
constexpr A Constrain(const A &amt, const B &low, const C &high)
値を指定された範囲内に収める (std::clamp)
Definition Math.hpp:68
Vec3 Vec3
Definition Vector3D.hpp:20
constexpr double Map(const double value, const double inputMin, const double inputMax, const double outputMin, const double outputMax)
数値をある範囲から別の範囲に再マッピングする
Definition Math.hpp:136
極座標系
Definition Polar.hpp:21
二次元ベクトル
Definition Vector2D.hpp:22
Vec2 & rotateAt(const Vec2 &center, ValueType angle) noexcept
指定された点を中心に時計回りに回転させる
Definition Vector2D.hpp:164
friend constexpr bool operator==(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:83
Vec2 & operator/=(ValueType rhs) noexcept
Definition Vector2D.hpp:81
friend constexpr Vec2 operator*(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:60
Vec2 scaledLength(ValueType length) const noexcept
長さを変更したベクトルを返す
Definition Vector2D.hpp:235
Vec2(const Vec2 &)=default
デフォルトコピーコンストラクタ
ValueType angleAt(const Vec2 &rhs) const noexcept
指定された点からの角度を求める
Definition Vector2D.hpp:189
ValueType y
Y成分
Definition Vector2D.hpp:31
Vec2 & operator-=(ValueType rhs) noexcept
Definition Vector2D.hpp:79
Vec2 & operator/=(const Vec2 &rhs) noexcept
Definition Vector2D.hpp:76
constexpr Vec2() noexcept
デフォルトコンストラクタ
Definition Vector2D.hpp:37
Udon::Vec3 xy0() const noexcept
Definition Vector2D.hpp:13
Vec2 & operator+=(const Vec2 &rhs) noexcept
Definition Vector2D.hpp:73
Vec2 & replaceY(ValueType newValue) noexcept
Definition Vector2D.hpp:118
ValueType length() const noexcept
原点からの距離を求める
Definition Vector2D.hpp:221
friend constexpr Vec2 operator-(ValueType lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:69
Vec2 & scaleLength(ValueType length) noexcept
ベクトルの長さを指定された値にする
Definition Vector2D.hpp:243
friend constexpr Vec2 operator-(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:59
constexpr bool isZero() const noexcept
ゼロベクトルであるかを返す
Definition Vector2D.hpp:101
constexpr Vec2 mapped(ValueType fromMin, ValueType fromMax, ValueType toMin, ValueType toMax) const noexcept
各要素をある範囲から別の範囲に再マップしたベクトルを返す
Definition Vector2D.hpp:254
Vec2 & replaceX(Visitor &&visitor) noexcept
Definition Vector2D.hpp:125
friend constexpr Vec2 operator-(const Vec2 &lhs, ValueType rhs) noexcept
Definition Vector2D.hpp:64
Vec2 rotated(ValueType angle) const noexcept
原点を中心に回転したベクトルを返す
Definition Vector2D.hpp:172
Vec2 & map(ValueType fromMin, ValueType fromMax, ValueType toMin, ValueType toMax) noexcept
各要素をある範囲から別の範囲に再マップする
Definition Vector2D.hpp:268
Vec2 & operator*=(ValueType rhs) noexcept
Definition Vector2D.hpp:80
Vec2 & operator+=(ValueType rhs) noexcept
Definition Vector2D.hpp:78
friend constexpr Vec2 operator/(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:61
ValueType angle() const noexcept
原点からの時計回りの角度を求める
Definition Vector2D.hpp:205
constexpr ValueType lengthSq() const noexcept
原点からの距離の二乗を求める
Definition Vector2D.hpp:228
Vec2 & clamp(ValueType min, ValueType max) noexcept
各要素に制限をかける
Definition Vector2D.hpp:307
constexpr Vec2(ValueType x, ValueType y) noexcept
コンストラクタ
Definition Vector2D.hpp:46
friend constexpr Vec2 operator+(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:58
constexpr Vec2 clamped(ValueType min, ValueType max) const noexcept
各要素に制限をかけたベクトルを返す
Definition Vector2D.hpp:298
ValueType x
X成分
Definition Vector2D.hpp:28
Vec2 & operator=(const Vec2 &)=default
デフォルトコピー代入演算子
friend constexpr Vec2 operator+(ValueType lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:68
Vec2 normalized() const noexcept
正規化したベクトルを返す
Definition Vector2D.hpp:275
double ValueType
要素の型
Definition Vector2D.hpp:25
Udon::Polar toPolar() const noexcept
Definition Vector2D.hpp:18
friend constexpr Vec2 operator/(ValueType lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:71
friend constexpr bool operator!=(const Vec2 &lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:84
Vec2 & replaceY(Visitor &&visitor) noexcept
Definition Vector2D.hpp:132
ValueType distanceFrom(const Vec2 &rhs) const noexcept
指定された点からの距離を求める
Definition Vector2D.hpp:213
friend constexpr Vec2 operator/(const Vec2 &lhs, ValueType rhs) noexcept
Definition Vector2D.hpp:66
static constexpr size_t Dimension
次元数
Definition Vector2D.hpp:34
Vec2 & replaceX(ValueType newValue) noexcept
Definition Vector2D.hpp:112
friend constexpr Vec2 operator*(ValueType lhs, const Vec2 &rhs) noexcept
Definition Vector2D.hpp:70
Vec2 & rotate(ValueType angle) noexcept
原点を中心に回転させる
Definition Vector2D.hpp:180
Vec2 & normalize() noexcept
ベクトルを正規化する
Definition Vector2D.hpp:289
friend constexpr Vec2 operator*(const Vec2 &lhs, ValueType rhs) noexcept
Definition Vector2D.hpp:65
UDON_ENUMERABLE(x, y)
OpenSiv3D との連携
friend constexpr Vec2 operator+(const Vec2 &lhs, ValueType rhs) noexcept
Definition Vector2D.hpp:63
static constexpr Vec2 Zero() noexcept
ゼロベクトルを返す
Definition Vector2D.hpp:95
Vec2 rotatedAt(const Vec2 &center, ValueType angle) const noexcept
指定された点を中心に時計回りに回転したベクトルを返す
Definition Vector2D.hpp:152
void clear() noexcept
値クリア
Definition Vector2D.hpp:107
Vec2 abs() const noexcept
絶対値を取る
Definition Vector2D.hpp:140
Vec2 & operator-=(const Vec2 &rhs) noexcept
Definition Vector2D.hpp:74
Vec2 & operator*=(const Vec2 &rhs) noexcept
Definition Vector2D.hpp:75
三次元ベクトル
Definition Vector3D.hpp:20