Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 244 | chris | 1 | #ifndef __SEXYVECTOR_H__ |
| 2 | #define __SEXYVECTOR_H__ |
||
| 3 | |||
| 4 | #include <math.h> |
||
| 5 | |||
| 6 | namespace Sexy |
||
| 7 | { |
||
| 8 | |||
| 9 | /////////////////////////////////////////////////////////////////////////////// |
||
| 10 | /////////////////////////////////////////////////////////////////////////////// |
||
| 11 | class SexyVector2 |
||
| 12 | { |
||
| 13 | public: |
||
| 14 | float x,y; |
||
| 15 | |||
| 16 | public: |
||
| 17 | SexyVector2() : x(0), y(0) { } |
||
| 18 | SexyVector2(float theX, float theY) : x(theX), y(theY) { } |
||
| 19 | |||
| 20 | float Dot(const SexyVector2 &v) const { return x*v.x + y*v.y; } |
||
| 21 | SexyVector2 operator+(const SexyVector2 &v) const { return SexyVector2(x+v.x, y+v.y); } |
||
| 22 | SexyVector2 operator-(const SexyVector2 &v) const { return SexyVector2(x-v.x, y-v.y); } |
||
| 23 | SexyVector2 operator-() const { return SexyVector2(-x, -y); } |
||
| 24 | SexyVector2 operator*(float t) const { return SexyVector2(t*x, t*y); } |
||
| 25 | SexyVector2 operator/(float t) const { return SexyVector2(x/t, y/t); } |
||
| 26 | void operator+=(const SexyVector2 &v) { x+=v.x; y+=v.y; } |
||
| 27 | void operator-=(const SexyVector2 &v) { x-=v.x; y-=v.y; } |
||
| 28 | void operator*=(float t) { x*=t; y*=t; } |
||
| 29 | void operator/=(float t) { x/=t; y/=t; } |
||
| 30 | |||
| 31 | bool operator==(const SexyVector2 &v) { return x==v.x && y==v.y; } |
||
| 32 | bool operator!=(const SexyVector2 &v) { return x!=v.x || y!=v.y; } |
||
| 33 | |||
| 34 | float Magnitude() const { return sqrtf(x*x + y*y); } |
||
| 35 | float MagnitudeSquared() const { return x*x+y*y; } |
||
| 36 | |||
| 37 | SexyVector2 Normalize() const |
||
| 38 | { |
||
| 39 | float aMag = Magnitude(); |
||
| 40 | return aMag!=0 ? (*this)/aMag : *this; |
||
| 41 | } |
||
| 42 | |||
| 43 | SexyVector2 Perp() const |
||
| 44 | { |
||
| 45 | return SexyVector2(-y, x); |
||
| 46 | } |
||
| 47 | }; |
||
| 48 | |||
| 49 | /////////////////////////////////////////////////////////////////////////////// |
||
| 50 | /////////////////////////////////////////////////////////////////////////////// |
||
| 51 | class SexyVector3 |
||
| 52 | { |
||
| 53 | public: |
||
| 54 | float x,y,z; |
||
| 55 | |||
| 56 | public: |
||
| 57 | SexyVector3() : x(0), y(0), z(0) { } |
||
| 58 | SexyVector3(float theX, float theY, float theZ) : x(theX), y(theY), z(theZ) { } |
||
| 59 | |||
| 60 | float Dot(const SexyVector3 &v) const { return x*v.x + y*v.y + z*v.z; } |
||
| 61 | SexyVector3 Cross(const SexyVector3 &v) const { return SexyVector3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); } |
||
| 62 | SexyVector3 operator+(const SexyVector3 &v) const { return SexyVector3(x+v.x, y+v.y, z+v.z); } |
||
| 63 | SexyVector3 operator-(const SexyVector3 &v) const { return SexyVector3(x-v.x, y-v.y, z-v.z); } |
||
| 64 | SexyVector3 operator*(float t) const { return SexyVector3(t*x, t*y, t*z); } |
||
| 65 | SexyVector3 operator/(float t) const { return SexyVector3(x/t, y/t, z/t); } |
||
| 66 | float Magnitude() const { return sqrtf(x*x + y*y + z*z); } |
||
| 67 | |||
| 68 | SexyVector3 Normalize() const |
||
| 69 | { |
||
| 70 | float aMag = Magnitude(); |
||
| 71 | return aMag!=0 ? (*this)/aMag : *this; |
||
| 72 | } |
||
| 73 | }; |
||
| 74 | |||
| 75 | }; |
||
| 76 | |||
| 77 | #endif |