-
Notifications
You must be signed in to change notification settings - Fork 57
/
Vector2D.h
184 lines (147 loc) · 3.9 KB
/
Vector2D.h
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once
typedef float vec_t;
// 2D Vector
class Vector2D
{
public:
// Members
vec_t x, y;
// Construction/destruction:
Vector2D(void);
Vector2D(vec_t X, vec_t Y);
Vector2D(vec_t* clr);
Vector2D(const Vector2D& vOther)
{
x = vOther.x; y = vOther.y;
}
// Initialization
void Init(vec_t ix = 0.0f, vec_t iy = 0.0f);
// TODO (Ilya): Should there be an init that takes a single float for consistency?
// Got any nasty NAN's?
bool IsValid() const;
void Invalidate();
// array access...
vec_t operator[](int i) const;
vec_t& operator[](int i);
// Base address...
vec_t* Base();
vec_t const* Base() const;
// Initialization methods
void Random(vec_t minVal, vec_t maxVal);
void Zero(); ///< zero out a vector
// equality
bool operator==(const Vector2D& v) const;
bool operator!=(const Vector2D& v) const;
// arithmetic operations
Vector2D& operator+=(const Vector2D& v)
{
x += v.x; y += v.y;
return *this;
}
Vector2D& operator-=(const Vector2D& v)
{
x -= v.x; y -= v.y;
return *this;
}
Vector2D& operator*=(float fl)
{
x *= fl;
y *= fl;
return *this;
}
Vector2D& operator*=(const Vector2D& v)
{
x *= v.x;
y *= v.y;
return *this;
}
Vector2D& operator/=(const Vector2D& v)
{
x /= v.x;
y /= v.y;
return *this;
}
// this ought to be an opcode.
Vector2D& operator+=(float fl)
{
x += fl;
y += fl;
return *this;
}
// this ought to be an opcode.
Vector2D& operator/=(float fl)
{
x /= fl;
y /= fl;
return *this;
}
Vector2D& operator-=(float fl)
{
x -= fl;
y -= fl;
return *this;
}
// negate the vector components
void Negate();
// Get the vector's magnitude.
vec_t Length() const;
// Get the vector's magnitude squared.
vec_t LengthSqr(void) const
{
return (x * x + y * y);
}
// return true if this vector is (0,0,0) within tolerance
bool IsZero(float tolerance = 0.01f) const
{
return (x > -tolerance && x < tolerance&&
y > -tolerance && y < tolerance);
}
vec_t NormalizeInPlace();
Vector2D Normalized() const;
bool IsLengthGreaterThan(float val) const;
bool IsLengthLessThan(float val) const;
// check if a vector is within the box defined by two other vectors
bool WithinAABox(Vector2D const& boxmin, Vector2D const& boxmax);
// Get the distance from this vector to the other one.
vec_t DistTo(const Vector2D& vOther) const;
// Get the distance from this vector to the other one squared.
// NJS: note, VC wasn't inlining it correctly in several deeply nested inlines due to being an 'out of line' .
// may be able to tidy this up after switching to VC7
vec_t DistToSqr(const Vector2D& vOther) const
{
Vector2D delta;
delta.x = x - vOther.x;
delta.y = y - vOther.y;
return delta.LengthSqr();
}
// Copy
void CopyToArray(float* rgfl) const;
// Multiply, add, and assign to this (ie: *this = a + b * scalar). This
// is about 12% faster than the actual vector equation (because it's done per-component
// rather than per-vector).
void MulAdd(const Vector2D& a, const Vector2D& b, float scalar);
// Dot product.
vec_t Dot(const Vector2D& vOther) const;
// assignment
Vector2D& operator=(const Vector2D& vOther);
// 2d
vec_t Length2D(void) const;
vec_t Length2DSqr(void) const;
/// Get the component of this vector parallel to some other given vector
Vector2D ProjectOnto(const Vector2D& onto);
// copy constructors
// Vector2D(const Vector2D &vOther);
// arithmetic operations
Vector2D operator-(void) const;
Vector2D operator+(const Vector2D& v) const;
Vector2D operator-(const Vector2D& v) const;
Vector2D operator*(const Vector2D& v) const;
Vector2D operator/(const Vector2D& v) const;
Vector2D operator*(float fl) const;
Vector2D operator/(float fl) const;
// Cross product between two vectors.
Vector2D Cross(const Vector2D& vOther) const;
// Returns a vector with the min or max in X, Y, and Z.
Vector2D Min(const Vector2D& vOther) const;
Vector2D Max(const Vector2D& vOther) const;
};