KisaragiLibrary
 
読み取り中…
検索中…
一致する文字列を見つけられません
Primitive.hpp
[詳解]
1#pragma once
2
3/**
4 * @file Primitive.hpp
5 * @brief 図形の形状を表すためのクラスを提供するヘッダ
6 * @author 樺澤陽向
7 * @date 2025/05/08
8 * @since ver1.0.0 Bool型削除による変更
9 */
10
11#include "vector"
12#include <cmath>
14
15using std::vector;
16
17namespace Kisaragi_Lib
18{
19 //この変数以下の誤差は無視して、等しいとみなす.
20 const static double EPSILON = 0.000001;
21
22 /// <summary>
23 /// 点を表すプリミティブ型
24 /// </summary>
25 /// <typeparam name="T">座標の型</typeparam>
26 /// @since ver1.0.0 コメント追加
27 template<typename T>
28 struct Point2D
29 {
30 T x, y;
31
32 Point2D() :x(0), y(0) {}
33 Point2D(T _x, T _y) :x(_x), y(_y) {}
34
35#pragma region 算術演算子 %抜き
36 Point2D operator + (const Point2D _num) const
37 {
38 return Point2D(x + _num.x, y + _num.y);
39 }
40
41 Point2D operator - (const Point2D _num) const
42 {
43 return Point2D(x - _num.x, y - _num.y);
44 }
45
46 Point2D operator * (const Point2D _num) const
47 {
48 return Point2D(x * _num.x, y * _num.y);
49 }
50
51 Point2D operator / (const Point2D _num) const
52 {
53 return Point2D(x / _num.x, y / _num.y);
54 }
55#pragma endregion
56
57#pragma region 算術代入子 %抜き
58 const Point2D operator += (const Point2D _num)
59 {
60 x += _num.x;
61 y += _num.y;
62
63 return *this;
64 }
65
66 const Point2D operator -= (const Point2D _num)
67 {
68 x -= _num.x;
69 y -= _num.y;
70
71 return *this;
72 }
73
74 const Point2D operator *= (const Point2D _num)
75 {
76 x *= _num.x;
77 y *= _num.y;
78
79 return *this;
80 }
81
82 const Point2D operator /= (const Point2D _num)
83 {
84 x /= _num.x;
85 y /= _num.y;
86
87 return *this;
88 }
89#pragma endregion
90
91#pragma region スカラー
92 Point2D operator * (T _num) const
93 {
94 return Point2D(x * _num, y * _num);
95 }
96
97 Point2D operator / (T _num) const
98 {
99 return Point2D(x / _num, y / _num);
100 }
101
102 const Point2D operator *= (T _num)
103 {
104 x *= _num;
105 y *= _num;
106
107 return *this;
108 }
109
110 const Point2D operator /= (T _num)
111 {
112 x /= _num;
113 y /= _num;
114
115 return *this;
116 }
117#pragma endregion
118
119 //符号反転演算子
121 {
122 return Point2D(-x, -y);
123 }
124
125 //内積/ドット積
126 T Dot(const Point2D _num) const
127 {
128 return x * _num.x + y * _num.y;
129 }
130
131 // クロス積/外積亜種?
132 T Cross(const Point2D _num) const
133 {
134 return x * _num.y - y * _num.x;
135 }
136
137 //ベクトルの長さを求める
138 T Length() const
139 {
140 return std::sqrt(Math::Pow(x,2) + Math::Pow(y,2));
141
142 }
143
144 /// <summary>
145 /// ベクトルを正規化する.
146 /// </summary>
147 /// <typeparam name="type">ベクトルの型</typeparam>
148 /// <param name="_vec">正規化したいベクトル</param>
149 /// <returns>正規化したベクトル</returns>
151 {
152 T vecLength = sqrt((x * x) + (y * y));
153
154 //正規化されている(ベクトルの長さが1)なら.
155 if (EpsilonIf(vecLength, 1, EPSILON))
156 {
157 //処理しない
158 return *this;
159 }
160
161 //正規化処理.
162 if (x != 0)
163 {
164 x = (x / vecLength);
165 }
166
167 if (y != 0)
168 {
169 y = (y / vecLength);
170 }
171
172 return *this;
173
174 }
175
177 {
178 Point2D tmp(x, y);
179 tmp.Normalize();
180
181 return tmp;
182 }
183
184 };
185
186 /// <summary>
187 /// サイズを表すプリミティブ型
188 /// 実体はPoint2D<T>と変わりありません
189 /// </summary>
190 /// <typeparam name="T">サイズの型</typeparam>
191 /// @since ver1.0.0 コメント追加
192 template<typename T>
194
195
196 /// <summary>
197 /// ベクトルを表すプリミティブ型
198 /// </summary>
199 /// <typeparam name="T">ベクトルの型</typeparam>
200 /// @since ver1.0.0 コメント追加
201 template<typename T>
202 struct Vector2D : public Point2D<T>
203 {
204 using Point2D<T>::x;
205 using Point2D<T>::y;
206
207 using Point2D<T>::Cross;
208 using Point2D<T>::Dot;
209 using Point2D<T>::GetNormalize;
210
211
212 Vector2D() : Point2D<T>(){}
213 Vector2D(T _x, T _y) : Point2D<T>(_x, _y) {}
214
216 {
217 x = _p.x;
218 y = _p.y;
219
220 return *this;
221 }
222
223 //Point2Dをcastできるように
224 explicit Vector2D(const Point2D<T>& _p) : Point2D<T>(_p.x, _p.y) {}
225
226 //大体垂直か調べる
227 bool isVertical(const Vector2D _vec) const
228 {
229 T dot = Dot(_vec);
230 return IsRange(dot, -EPSILON, EPSILON);
231 }
232
233 //大体平行か調べる
234 bool isParallel(const Vector2D _vec) const
235 {
236 T cross = Cross(_vec);
237 return IsRange(cross, -EPSILON, EPSILON);
238 }
239
240 // 鋭角関係にあるか
241 bool isSharpAngle(const Vector2D _vec) const
242 {
243 return (Dot(_vec) >= 0.0f);
244 }
245
246 /// <summary>
247 /// ベクトルから法線ベクトルを求める
248 /// </summary>
249 /// <typeparam name="T">Vector2Dの型</typeparam>
250 /// <param name="_clockwise">時計回りの法線ベクトルにするか</param>
251 /// <returns>法線ベクトル</returns>
252 static Vector2D<T> GetNormalVector(Vector2D<T> _vec, bool _clockwise = true)
253 {
254 Vector2D<T> vec;
255
256 //時計回りの法線ベクトルが欲しいなら
257 if (_clockwise)
258 {
259 vec = Vector2D<T>(-_vec.y, _vec.x);
260 }
261 else
262 {
263 vec = Vector2D<T>(_vec.y, -_vec.x);
264 }
265
266 vec.Normalize();
267
268 return vec;
269 }
270
271
272 /// <summary>
273 /// 2点から方向ベクトル(非正規)を得る
274 /// </summary>
275 /// <typeparam name="Type">座標の型</typeparam>
276 /// <param name="_startPoint">始点</param>
277 /// <param name="_endPoint">終点</param>
278 /// <returns>始点か終点 への方向ベクトル(非正規)</returns>
279 template<typename Type>
280 static Vector2D<Type> PointToVector(const Point2D<Type> _startPoint, const Point2D<Type> _endPoint)
281 {
282 return Vector2D<Type>(_endPoint - _startPoint);
283 }
284
285 /// <summary>
286 /// 正規化された方向ベクトルから角度を求める.
287 /// </summary>
288 /// <typeparam name="T">ベクトルの型</typeparam>
289 /// <param name="_vec">正規化された方向ベクトル</param>
290 /// <returns>角度</returns>
291 static double ToDegree(Vector2D<T> _vec)
292 {
293 //正規化
294 _vec.Normalize();
295
296 return Math::RadianToDegree(atan2(_vec.y, _vec.x));
297 }
298
299 /// <summary>
300 /// 自身の角度を求める.
301 /// </summary>
302 /// <typeparam name="T">ベクトルの型</typeparam>
303 /// <param name="_vec">正規化された方向ベクトル</param>
304 /// <returns>角度</returns>
305 double Degree()
306 {
307 //正規化
308 Vector2D<T> _vec = *this;
309 _vec = GetNormalize();
310
311 return Math::RadianToDegree(atan2(_vec.y, _vec.x));
312 }
313
314 /// <summary>
315 /// 方向ベクトルからラジアン値を求める.
316 /// </summary>
317 /// <typeparam name="T">ベクトルの型</typeparam>
318 /// <param name="_vec">正規化された方向ベクトル</param>
319 /// <returns>ラジアン値</returns>
320 template<typename T>
321 static double ToRadian(Vector2D<T> _vec)
322 {
323 //正規化
324 _vec.Normalize();
325
326 return atan2(_vec.y, _vec.x);
327 }
328
329 /// <summary>
330 /// 方向ベクトルからラジアン値を求める.
331 /// </summary>
332 /// <param name="_vec">正規化された方向ベクトル</param>
333 /// <returns>ラジアン値</returns>
334 double ToRadian()
335 {
336 Vector2D<T> _vec = *this;
337 _vec.Normalize();
338
339 return atan2(_vec.y, _vec.x);
340 }
341
342 /// <summary>
343 /// ラジアンからベクトルを取得する.
344 /// 0度がどこかは知らない.
345 /// </summary>
346 /// <typeparam name="type">ベクトルの型</typeparam>
347 /// <param name="_radian">ラジアン値</param>
348 /// <returns>取得したベクトル</returns>
349 template<typename type>
350 static Vector2D<type> RadianToVector(const type _radian)
351 {
352 Vector2D<type> tmp;
353 tmp.x = cos(_radian);
354 tmp.y = sin(_radian);
355
356 return Vector2D<type>{ tmp.Normalize()};
357 }
358
359
360 };
361
362 /// <summary>
363 /// 始点と方向ベクトルで示される無限の直線を表すプリミティブ型
364 /// </summary>
365 /// <typeparam name="T">各値の数値型の種類</typeparam>
366 /// @since ver1.0.0 コメント追加
367 template<typename T>
368 struct Line2D
369 {
372
373 Line2D() : startPoint(0, 0), direction(0, 0) {}
374
375 Line2D(Point2D<T> _startPoint,Vector2D<T> _direction) :
376 startPoint(_startPoint), direction(_direction)
377 {
378 }
379
381 {
382 Point2D<T> tmp = direction;
383
384 //方向ベクトルの長さを求める.
385 tmp *= _length;
386
387 //始点分ずらす.
388 tmp += startPoint;
389
390 return tmp;
391 }
392
393 };
394
395 /// <summary>
396 /// 始点と方向ベクトルで示される有限の直線を表すプリミティブ型
397 /// </summary>
398 /// <typeparam name="T"></typeparam>
399 /// @since ver1.0.0 コメント追加
400 template<typename T>
401 struct Segment2D : public Line2D<T>
402 {
403 using Line2D<T>::startPoint;
404 using Line2D<T>::direction;
405
406 Segment2D() : Line2D<T>() {}
407
408 Segment2D(Point2D<T> _startPoint, Vector2D<T> _direction) :
409 Line2D<T>(_startPoint, _direction){}
410
412 {
414 }
415
416 };
417
418 /// <summary>
419 /// 円を表すプリミティブ型
420 /// </summary>
421 /// <typeparam name="T"></typeparam>
422 /// @since ver1.0.0 コメント追加
423 template<typename T>
424 struct Circle2D
425 {
428
429 Circle2D() : center(0, 0), radius(0) {}
430
431 Circle2D(Point2D<T> _center,T _radius) : center(_center), radius(_radius) {}
432 };
433
434 /// <summary>
435 /// 楕円形を表すプリミティブ型
436 /// </summary>
437 /// <typeparam name="T"></typeparam>
438 /// @since ver1.0.0 コメント追加
439 template<typename T>
441 {
444
446
447 Capsule2D(Segment2D<T> _segment,T ) :segment(_segment), radius(0) {}
448
449 Capsule2D(Point2D<T> _point, Vector2D<T> _vector,T _radius) :segment(), radius() {}
450
451 };
452
453 /// <summary>
454 /// 軸平行境界ボックス
455 /// </summary>
456 /// <typeparam name="T"></typeparam>
457 /// @since ver1.0.0 コメント追加
458 template<typename T>
459 struct AABB
460 {
461 //中心座標のずれ(ローカル)
463
465
466 AABB(Point2D<T> _offset, Scale2D<T> _size) : offset(_offset), size(_size) {}
467 AABB(Scale2D<T> _size) : offset(Point2D<T>(0,0)), size(_size) {}
468
469
470 //TODO debug
472 {
473 //center - {(size / 2) - offset} == 左上座標 (ずれ込み)
474 // {}内の部分を計算で求めてる
475 //左上座標 - ずれ
476 return -(size / 2) - offset;
477 }
478
480 {
481 return (size / 2) + offset;
482 }
483 };
484}
独自のMathを提供します
static T Pow(const T &_num, const unsigned int &_cnt)
べき乗,累乗
Definition KisaragiMath.hpp:126
static double RadianToDegree(const double &_radian)
ラジアン値を角度に変換する.
Definition KisaragiMath.hpp:47
Definition Accessor.hpp:110
static bool IsRange(T _val, T _rangeMin, T _rangeMax)
valが範囲内か調べる
Definition utility.hpp:59
Point2D< T > Scale2D
サイズを表すプリミティブ型 実体はPoint2D<T>と変わりありません
Definition Primitive.hpp:193
static bool EpsilonIf(T _val01, T _val02, T _epsilon)
_valの差が_許容範囲内か調べる
Definition utility.hpp:44
static const double EPSILON
Definition Primitive.hpp:20
AABB(Scale2D< T > _size)
Definition Primitive.hpp:467
Point2D< T > RightBottomPoint()
Definition Primitive.hpp:479
AABB(Point2D< T > _offset, Scale2D< T > _size)
Definition Primitive.hpp:466
Point2D< T > offset
Definition Primitive.hpp:462
Point2D< T > LeftTopPoint()
Definition Primitive.hpp:471
Scale2D< T > size
Definition Primitive.hpp:464
T radius
Definition Primitive.hpp:443
Capsule2D()
Definition Primitive.hpp:445
Segment2D< T > segment
Definition Primitive.hpp:442
Capsule2D(Point2D< T > _point, Vector2D< T > _vector, T _radius)
Definition Primitive.hpp:449
Capsule2D(Segment2D< T > _segment, T)
Definition Primitive.hpp:447
Circle2D(Point2D< T > _center, T _radius)
Definition Primitive.hpp:431
Circle2D()
Definition Primitive.hpp:429
Point2D< T > center
Definition Primitive.hpp:426
T radius
Definition Primitive.hpp:427
Line2D()
Definition Primitive.hpp:373
Point2D< T > startPoint
Definition Primitive.hpp:370
Line2D(Point2D< T > _startPoint, Vector2D< T > _direction)
Definition Primitive.hpp:375
Point2D< T > GetPoint(T _length)
Definition Primitive.hpp:380
Vector2D< T > direction
Definition Primitive.hpp:371
点を表すプリミティブ型
Definition Primitive.hpp:29
Point2D GetNormalize() const
Definition Primitive.hpp:176
const Point2D operator+=(const Point2D _num)
Definition Primitive.hpp:58
Point2D operator*(const Point2D _num) const
Definition Primitive.hpp:46
Point2D operator+(const Point2D _num) const
Definition Primitive.hpp:36
T y
Definition Primitive.hpp:30
T Dot(const Point2D _num) const
Definition Primitive.hpp:126
const Point2D operator-=(const Point2D _num)
Definition Primitive.hpp:66
T x
Definition Primitive.hpp:30
Point2D< T > Normalize()
ベクトルを正規化する.
Definition Primitive.hpp:150
Point2D operator/(const Point2D _num) const
Definition Primitive.hpp:51
T Cross(const Point2D _num) const
Definition Primitive.hpp:132
T Length() const
Definition Primitive.hpp:138
const Point2D operator*=(const Point2D _num)
Definition Primitive.hpp:74
Point2D operator-() const
Definition Primitive.hpp:120
Point2D(T _x, T _y)
Definition Primitive.hpp:33
const Point2D operator/=(const Point2D _num)
Definition Primitive.hpp:82
Point2D()
Definition Primitive.hpp:32
始点と方向ベクトルで示される有限の直線を表すプリミティブ型
Definition Primitive.hpp:402
Segment2D()
Definition Primitive.hpp:406
Segment2D(Point2D< T > _startPoint, Vector2D< T > _direction)
Definition Primitive.hpp:408
Point2D< T > EndPoint() const
Definition Primitive.hpp:411
ベクトルを表すプリミティブ型
Definition Primitive.hpp:203
static Vector2D< type > RadianToVector(const type _radian)
ラジアンからベクトルを取得する. 0度がどこかは知らない.
Definition Primitive.hpp:350
bool isParallel(const Vector2D _vec) const
Definition Primitive.hpp:234
double Degree()
自身の角度を求める.
Definition Primitive.hpp:305
Vector2D()
Definition Primitive.hpp:212
static double ToDegree(Vector2D< T > _vec)
正規化された方向ベクトルから角度を求める.
Definition Primitive.hpp:291
static Vector2D< T > GetNormalVector(Vector2D< T > _vec, bool _clockwise=true)
ベクトルから法線ベクトルを求める
Definition Primitive.hpp:252
double ToRadian()
方向ベクトルからラジアン値を求める.
Definition Primitive.hpp:334
Vector2D(const Point2D< T > &_p)
Definition Primitive.hpp:224
bool isVertical(const Vector2D _vec) const
Definition Primitive.hpp:227
static double ToRadian(Vector2D< T > _vec)
方向ベクトルからラジアン値を求める.
Definition Primitive.hpp:321
Vector2D(T _x, T _y)
Definition Primitive.hpp:213
Vector2D operator=(Point2D< T > _p)
Definition Primitive.hpp:215
static Vector2D< Type > PointToVector(const Point2D< Type > _startPoint, const Point2D< Type > _endPoint)
2点から方向ベクトル(非正規)を得る
Definition Primitive.hpp:280
bool isSharpAngle(const Vector2D _vec) const
Definition Primitive.hpp:241