-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector2D_InlineDef.h
More file actions
112 lines (90 loc) · 2.25 KB
/
Vector2D_InlineDef.h
File metadata and controls
112 lines (90 loc) · 2.25 KB
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
#pragma once
#include "Vector2D.h"
#include <algorithm>
#include <math.h>
void Vector2D::Normalize()
{
double length = GetLength();
if (length == 0)
return;
_x /= length;
_y /= length;
}
//ベクトルを指定の角度だけ回転させる
void Vector2D::Rotate(double theta)
{
double length = GetLength();
//現在のx軸(1, 0)との角度を算出
//b = (1, 0)
//cos = (a.x * b.x + a.y * b.y) / (|a| * |b|)
// = a.x / a.length
double rad = acos(_x / length);
//acos値だけでは時計・反時計まわりが判別できないので
//asin値もとって確認
//sin = a.y / a.length
if (asin(_y / length) < 0)
rad *= -1.0;
//現在の角度からさらに回転させる
//theta += rad;
_x = length * cos(rad + theta * (3.14159265359 / 180.0));
_y = length * sin(rad + theta * (3.14159265359 / 180.0));
}
void Vector2D::Swap()
{
std::swap(_x, _y);
}
void Vector2D::Set(const Vector2D &start, const Vector2D &end)
{
_x = end._x - start._x;
_y = end._y - start._y;
}
void Vector2D::Set(double elemX, double elemY)
{
_x = elemX;
_y = elemY;
}
void Vector2D::Set(double startX, double startY, double endX, double endY)
{
_x = endX - startX;
_x = endY - startY;
}
double Vector2D::GetLength() const
{
return sqrt(_x * _x + _y * _y);
}
double Vector2D::GetSqLength() const
{
return (_x * _x + _y * _y);
}
//内積値を算出
double Vector2D::Dot(const Vector2D &vecA, const Vector2D &vecB)
{
return (vecA._x * vecB._x + vecA._y * vecB._y);
}
//クロス積を出す
double Vector2D::Cross(const Vector2D &vecA, const Vector2D &vecB)
{
return (vecA._x * vecB._y - vecA._y * vecB._x);
}
//Z方向のベクトル(0, 0, 1)と3次元上で外積計算し、算出されたベクトルを取得
Vector2D Vector2D::Cross(const Vector2D &vecA, bool useRightHandSystem)
{
//Vector3 c = (0, 0, 1) と交差判定
double z = (useRightHandSystem) ? 1.0 : -1.0;
//bx = ay * cz - cy * az = ay * cz;
//by = az * cy - cz * ax = -cz * ax;
//bz = ay * cx - cy * ax = 0;
return std::move(Vector2D(vecA._y * z, -vecA._x * z));
}
Vector2D Vector2D::GetNormalized()
{
Vector2D temp(_x, _y);
temp.Normalize();
return std::move(temp);
}
Vector2D Vector2D::GetRotated(double theta)
{
Vector2D temp(_x, _y);
temp.Rotate(theta);
return std::move(temp);
}