6 @maintainer Morgan McGuire, matrix@graphics3d.com
10 Copyright 2000-2006, Morgan McGuire.
17 #include "G3D/platform.h"
18 #include "G3D/g3dmath.h"
19 #include "Vector2int16.h"
29 Do not subclass-- this implementation makes assumptions about the
35 bool operator<(const Vector2
&) const;
36 bool operator>(const Vector2
&) const;
37 bool operator<=(const Vector2
&) const;
38 bool operator>=(const Vector2
&) const;
46 Vector2(float x
, float y
);
47 Vector2(float coordinate
[2]);
48 Vector2(double coordinate
[2]);
49 Vector2(const Vector2
& rkVector
);
50 Vector2(const class Vector2int16
& v
);
52 float& operator[] (int i
);
53 const float& operator[] (int i
) const;
55 operator const float* () const;
57 // assignment and comparison
58 Vector2
& operator= (const Vector2
& rkVector
);
59 bool operator== (const Vector2
& rkVector
) const;
60 bool operator!= (const Vector2
& rkVector
) const;
61 unsigned int hashCode() const;
62 bool fuzzyEq(const Vector2
& other
) const;
63 bool fuzzyNe(const Vector2
& other
) const;
64 /** Returns true if this vector has finite length */
65 bool isFinite() const;
67 /** Returns true if this vector has length == 0 */
70 /** Returns true if this vector has length == 1 */
73 // arithmetic operations
74 Vector2
operator+ (const Vector2
& rkVector
) const;
75 Vector2
operator- (const Vector2
& rkVector
) const;
76 Vector2
operator* (float fScalar
) const;
77 Vector2
operator* (const Vector2
& rkVector
) const;
78 Vector2
operator/ (const Vector2
& rkVector
) const;
79 Vector2
operator/ (float fScalar
) const;
80 Vector2
operator- () const;
82 inline float sum() const {
89 inline Vector2
lerp(const Vector2
& v
, float alpha
) const {
90 return (*this) + (v
- *this) * alpha
;
93 inline Vector2
clamp(const Vector2
& low
, const Vector2
& high
) const {
95 G3D::clamp(x
, low
.x
, high
.x
),
96 G3D::clamp(y
, low
.y
, high
.y
));
99 inline Vector2
clamp(float low
, float high
) const {
101 (float)G3D::clamp(x
, low
, high
),
102 (float)G3D::clamp(y
, low
, high
));
105 // arithmetic updates
106 Vector2
& operator+= (const Vector2
& rkVector
);
107 Vector2
& operator-= (const Vector2
& rkVector
);
108 Vector2
& operator*= (float fScalar
);
109 Vector2
& operator/= (float fScalar
);
110 Vector2
& operator*= (const Vector2
& rkVector
);
111 Vector2
& operator/= (const Vector2
& rkVector
);
114 float length() const;
115 Vector2
direction() const;
117 Potentially less accurate but faster than direction().
118 Only works if System::hasSSE is true.
120 Vector2
fastDirection() const {
124 float squaredLength () const;
125 float dot (const Vector2
& rkVector
) const;
126 float unitize (float fTolerance
= 1e-06);
128 Vector2
min(const Vector2
&v
) const;
129 Vector2
max(const Vector2
&v
) const;
131 // Random unit vector
132 static Vector2
random();
135 // Intentionally not inlined: see Matrix3::identity() for details.
136 static const Vector2
& zero();
137 inline static const Vector2
& one() { static const Vector2
v(1, 1); return v
; }
138 static const Vector2
& unitX();
139 static const Vector2
& unitY();
140 static const Vector2
& inf();
141 static const Vector2
& nan();
142 /** smallest (most negative) representable vector */
143 static const Vector2
& minFinite();
144 /** Largest representable vector */
145 static const Vector2
& maxFinite();
149 // Deprecated. See Matrix3::identity() for details.
150 /** @deprecated Use Vector2::zero() */
151 static const Vector2 ZERO
;
152 /** @deprecated Use Vector2::unitX() */
153 static const Vector2 UNIT_S
;
154 /** @deprecated Use Vector2::unitY() */
155 static const Vector2 UNIT_T
;
157 std::string
toString() const;
179 Vector4
xxxx() const;
180 Vector4
yxxx() const;
181 Vector4
xyxx() const;
182 Vector4
yyxx() const;
183 Vector4
xxyx() const;
184 Vector4
yxyx() const;
185 Vector4
xyyx() const;
186 Vector4
yyyx() const;
187 Vector4
xxxy() const;
188 Vector4
yxxy() const;
189 Vector4
xyxy() const;
190 Vector4
yyxy() const;
191 Vector4
xxyy() const;
192 Vector4
yxyy() const;
193 Vector4
xyyy() const;
194 Vector4
yyyy() const;
198 inline Vector2
operator*(double s
, const Vector2
& v
) {
202 inline Vector2
operator*(float s
, const Vector2
& v
) {
206 inline Vector2
operator*(int s
, const Vector2
& v
) {
211 inline unsigned int hashCode(const G3D::Vector2
& v
) {
216 inline Vector2::Vector2 () : x(0.0f
), y(0.0f
) {
220 inline Vector2::Vector2(float _x
, float _y
) : x(_x
), y(_y
) {
224 inline Vector2::Vector2 (float afCoordinate
[2]) {
231 inline Vector2::Vector2 (double afCoordinate
[2]) {
232 x
= (float)afCoordinate
[0];
233 y
= (float)afCoordinate
[1];
237 inline Vector2::Vector2 (const Vector2
& rkVector
) {
243 inline Vector2::Vector2 (const Vector2int16
& v
) : x(v
.x
), y(v
.y
) {
247 inline float& Vector2::operator[] (int i
) {
248 return ((float*)this)[i
];
252 inline const float& Vector2::operator[] (int i
) const {
253 return ((float*)this)[i
];
257 inline Vector2::operator float* () {
261 inline Vector2::operator const float* () const {
266 inline Vector2
& Vector2::operator= (const Vector2
& rkVector
) {
273 inline bool Vector2::operator== (const Vector2
& rkVector
) const {
274 return ( x
== rkVector
.x
&& y
== rkVector
.y
);
278 inline bool Vector2::operator!= (const Vector2
& rkVector
) const {
279 return ( x
!= rkVector
.x
|| y
!= rkVector
.y
);
283 inline Vector2
Vector2::operator+ (const Vector2
& rkVector
) const {
284 return Vector2(x
+ rkVector
.x
, y
+ rkVector
.y
);
288 inline Vector2
Vector2::operator- (const Vector2
& rkVector
) const {
289 return Vector2(x
- rkVector
.x
, y
- rkVector
.y
);
293 inline Vector2
Vector2::operator* (float fScalar
) const {
294 return Vector2(fScalar
*x
, fScalar
*y
);
299 inline Vector2
Vector2::operator- () const {
300 return Vector2( -x
, -y
);
305 inline Vector2
& Vector2::operator+= (const Vector2
& rkVector
) {
313 inline Vector2
& Vector2::operator-= (const Vector2
& rkVector
) {
321 inline Vector2
& Vector2::operator*= (float fScalar
) {
330 inline Vector2
& Vector2::operator*= (const Vector2
& rkVector
) {
338 inline Vector2
& Vector2::operator/= (const Vector2
& rkVector
) {
345 inline Vector2
Vector2::operator* (const Vector2
& rkVector
) const {
346 return Vector2(x
* rkVector
.x
, y
* rkVector
.y
);
351 inline Vector2
Vector2::operator/ (const Vector2
& rkVector
) const {
352 return Vector2(x
/ rkVector
.x
, y
/ rkVector
.y
);
356 inline float Vector2::squaredLength () const {
361 inline float Vector2::length () const {
362 return sqrtf(x
*x
+ y
*y
);
366 inline Vector2
Vector2::direction () const {
367 float lenSquared
= x
* x
+ y
* y
;
369 if (lenSquared
!= 1.0f
) {
370 return *this / sqrtf(lenSquared
);
378 inline float Vector2::dot (const Vector2
& rkVector
) const {
379 return x
*rkVector
.x
+ y
*rkVector
.y
;
384 inline Vector2
Vector2::min(const Vector2
&v
) const {
385 return Vector2(G3D::min(v
.x
, x
), G3D::min(v
.y
, y
));
390 inline Vector2
Vector2::max(const Vector2
&v
) const {
391 return Vector2(G3D::max(v
.x
, x
), G3D::max(v
.y
, y
));
396 inline bool Vector2::fuzzyEq(const Vector2
& other
) const {
397 return G3D::fuzzyEq((*this - other
).squaredLength(), 0);
402 inline bool Vector2::fuzzyNe(const Vector2
& other
) const {
403 return G3D::fuzzyNe((*this - other
).squaredLength(), 0);
408 inline bool Vector2::isFinite() const {
409 return G3D::isFinite(x
) && G3D::isFinite(y
);
414 inline bool Vector2::isZero() const {
415 return (x
== 0.0f
) && (y
== 0.0f
);
420 inline bool Vector2::isUnit() const {
421 return squaredLength() == 1.0f
;
426 // Intentionally outside namespace to avoid operator overloading confusion
427 inline G3D::Vector2
operator*(double s
, const G3D::Vector2
& v
) {
430 inline G3D::Vector2
operator*(int s
, const G3D::Vector2
& v
) {
435 inline unsigned int hashCode(const G3D::Vector2
& v
);