Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / include / g3dlite / G3D / Vector2.h
blob2d9d26626466295a7e0bcaa6e64939df81b9e7e0
1 /**
2 @file Vector2.h
4 2D vector class
6 @maintainer Morgan McGuire, matrix@graphics3d.com
8 @created 2001-06-02
9 @edited 2006-01-14
10 Copyright 2000-2006, Morgan McGuire.
11 All rights reserved.
14 #ifndef G3D_VECTOR2_H
15 #define G3D_VECTOR2_H
17 #include "G3D/platform.h"
18 #include "G3D/g3dmath.h"
19 #include "Vector2int16.h"
20 #include <string>
22 namespace G3D {
24 class Vector2;
25 class Vector3;
26 class Vector4;
28 /**
29 Do not subclass-- this implementation makes assumptions about the
30 memory layout.
32 class Vector2 {
33 private:
34 // Hidden operators
35 bool operator<(const Vector2&) const;
36 bool operator>(const Vector2&) const;
37 bool operator<=(const Vector2&) const;
38 bool operator>=(const Vector2&) const;
40 public:
41 // coordinates
42 float x, y;
44 // construction
45 Vector2();
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;
54 operator float* ();
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 */
68 bool isZero() const;
70 /** Returns true if this vector has length == 1 */
71 bool isUnit() const;
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 {
83 return x + y;
86 /**
87 Linear interpolation
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 {
94 return Vector2(
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 {
100 return Vector2(
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);
113 // vector operations
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 {
121 return direction();
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();
134 // Special values.
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;
159 // 2-char swizzles
161 Vector2 xx() const;
162 Vector2 yx() const;
163 Vector2 xy() const;
164 Vector2 yy() const;
166 // 3-char swizzles
168 Vector3 xxx() const;
169 Vector3 yxx() const;
170 Vector3 xyx() const;
171 Vector3 yyx() const;
172 Vector3 xxy() const;
173 Vector3 yxy() const;
174 Vector3 xyy() const;
175 Vector3 yyy() const;
177 // 4-char swizzles
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) {
199 return v * (float)s;
202 inline Vector2 operator*(float s, const Vector2& v) {
203 return v * s;
206 inline Vector2 operator*(int s, const Vector2& v) {
207 return v * (float)s;
211 inline unsigned int hashCode(const G3D::Vector2& v) {
212 return v.hashCode();
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]) {
225 x = afCoordinate[0];
226 y = afCoordinate[1];
231 inline Vector2::Vector2 (double afCoordinate[2]) {
232 x = (float)afCoordinate[0];
233 y = (float)afCoordinate[1];
237 inline Vector2::Vector2 (const Vector2& rkVector) {
238 x = rkVector.x;
239 y = rkVector.y;
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* () {
258 return (float*)this;
261 inline Vector2::operator const float* () const {
262 return (float*)this;
266 inline Vector2& Vector2::operator= (const Vector2& rkVector) {
267 x = rkVector.x;
268 y = rkVector.y;
269 return *this;
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) {
306 x += rkVector.x;
307 y += rkVector.y;
308 return *this;
313 inline Vector2& Vector2::operator-= (const Vector2& rkVector) {
314 x -= rkVector.x;
315 y -= rkVector.y;
316 return *this;
321 inline Vector2& Vector2::operator*= (float fScalar) {
322 x *= fScalar;
323 y *= fScalar;
324 return *this;
330 inline Vector2& Vector2::operator*= (const Vector2& rkVector) {
331 x *= rkVector.x;
332 y *= rkVector.y;
333 return *this;
338 inline Vector2& Vector2::operator/= (const Vector2& rkVector) {
339 x /= rkVector.x;
340 y /= rkVector.y;
341 return *this;
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 {
357 return x*x + y*y;
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);
371 } else {
372 return *this;
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) {
428 return v * (float)s;
430 inline G3D::Vector2 operator*(int s, const G3D::Vector2& v) {
431 return v * (float)s;
435 inline unsigned int hashCode(const G3D::Vector2& v);
438 #endif