-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.h
More file actions
123 lines (101 loc) · 3.31 KB
/
Vector2.h
File metadata and controls
123 lines (101 loc) · 3.31 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
113
114
115
116
117
118
119
120
121
122
123
/*
2D vector.
*/
#pragma once
class Vector2
{
public:
S3D_INLINE static const Vector2 Add(const Vector2 &A, const Vector2 &B) { return {A.x+B.x, A.y+B.y}; }
S3D_INLINE static const Vector2 Sub(const Vector2 &A, const Vector2 &B) { return {A.x-B.x, A.y-B.y}; }
S3D_INLINE static const Vector2 Mul(const Vector2 &A, const Vector2 &B) { return {A.x*B.x, A.y*B.y}; }
S3D_INLINE static const Vector2 Div(const Vector2 &A, const Vector2 &B) { return {A.x/B.x, A.y/B.y}; }
S3D_INLINE static const Vector2 Scale(const Vector2 &A, float B)
{
return { A.x*B, A.y*B };
}
S3D_INLINE static float Dot(const Vector2 &A, const Vector2 &B)
{
return A.x*B.x + A.y*B.y;
}
// AxB != BxA
// In 2D, the result is the signed magnitude of the imaginary perpendicular vector
// This is at the root of essentials like triangle rasterization and volume clipping (overlaps with what's done in RayTriangleIntersect())
S3D_INLINE static float Cross(const Vector2 &A, const Vector2 &B)
{
return A.x*B.y - B.x*A.y;
}
public:
float x, y;
explicit Vector2(float scalar) :
x(scalar), y(scalar) {}
Vector2(float x, float y) :
x(x), y(y) {}
const Vector2 operator +(const Vector2 &B) const { return Add(*this, B); }
const Vector2 operator +(float b) const { return Add(*this, Vector2(b)); }
const Vector2 operator -(const Vector2 &B) const { return Sub(*this, B); }
const Vector2 operator -(float b) const { return Sub(*this, Vector2(b)); }
const float operator *(const Vector2 &B) const { return Dot(*this, B); }
const Vector2 operator *(float b) const { return Scale(*this, b); }
const Vector2 operator /(const Vector2 &B) const { return Div(*this, B); }
const Vector2 operator /(float b) const { return Div(*this, Vector2(b)); }
Vector2& operator +=(const Vector2 &B) { return *this = *this + B; }
Vector2& operator +=(float b) { return *this = *this + b; }
Vector2& operator -=(const Vector2 &B) { return *this = *this - B; }
Vector2& operator -=(float b) { return *this = *this - b; }
Vector2& operator *=(float b) { return *this = *this * b; }
Vector2& operator /=(const Vector2 &B) { return *this = *this / B; }
Vector2& operator /=(float b) { return *this = *this / b; }
bool operator ==(const Vector2 &B) const
{
return comparef(x, B.x) && comparef(y, B.y);
}
bool operator !=(const Vector2 &B) const
{
return false == (*this == B);
}
bool operator <(const Vector2 &B) const
{
return LengthSq() < B.LengthSq();
}
S3D_INLINE float LengthSq() const
{
return Dot(*this, *this);
}
S3D_INLINE float Length() const
{
return sqrtf(Dot(*this, *this));
}
S3D_INLINE const Vector2 Normalized() const
{
auto result = *this;
result.Normalize();
return result;
}
S3D_INLINE void Normalize()
{
const float length = Length();
if (length > kEpsilon)
{
*this *= 1.f/length;
}
}
S3D_INLINE float Angle(const Vector2 &B) const
{
return acosf(Dot(*this, B));
}
// Project A (this) onto B
const Vector2 Project(const Vector2 &B) const
{
const Vector2 unitB = B.Normalized();
return unitB * Dot(*this, unitB);
}
S3D_INLINE const Vector2 Reflect(const Vector2 &normal) const
{
const float R = 2.f*Dot(*this, normal);
return *this - normal*R;
}
S3D_INLINE const Vector2 Perpendicular() const
{
return Vector2(-y, x);
}
};