00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #ifndef __VEC2_H__
00014 #define __VEC2_H__
00015
00016 #include <cmath>
00017
00018 namespace math
00019 {
00020 class vec2
00021 {
00022 public:
00023
00024 vec2(void):
00025 x(0.0f), y(0.0f)
00026 {}
00027
00028 vec2(float newX, float newY):
00029 x(newX), y(newY)
00030 {}
00031
00032 vec2(const float * rhs):
00033 x(*rhs), y((*rhs)+1)
00034 {}
00035
00036 vec2(const vec2 & rhs):
00037 x(rhs.x), y(rhs.y)
00038 {}
00039
00040 ~vec2() {}
00041
00042 void Set(float newX, float newY)
00043 { x=newX; y=newY; }
00044
00045
00046 void SetX(float newX) {x = newX;}
00047 void SetY(float newY) {y = newY;}
00048
00049 float GetX() const {return x;}
00050 float GetY() const {return y;}
00051
00052 void LoadZero(void);
00053 void LoadOne(void);
00054
00055 void Normalize();
00056 vec2 GetNormalized() const;
00057
00058 float GetLength() const
00059 { return (float)sqrt((x*x)+(y*y)); }
00060
00061 float GetSquaredLength() const
00062 { return (x*x)+(y*y); }
00063
00064
00065 vec2 lerp(const vec2 & v2, float factor) const
00066 { return (*this)*(1.0f-factor) + v2*factor; }
00067
00068 vec2 QuadraticInterpolate(const vec2 & v2, const vec2 & v3, float factor) const
00069 { return (*this)*(1.0f-factor)*(1.0f-factor) + 2*v2*factor*(1.0f-factor) + v3*factor*factor;}
00070
00071
00072
00073 vec2 operator+(const vec2 & rhs) const
00074 { return vec2(x + rhs.x, y + rhs.y); }
00075
00076 vec2 operator-(const vec2 & rhs) const
00077 { return vec2(x - rhs.x, y - rhs.y); }
00078
00079 vec2 operator*(const float rhs) const
00080 { return vec2(x*rhs, y*rhs); }
00081
00082 vec2 operator/(const float rhs) const
00083 { return (rhs==0) ? vec2(0.0f, 0.0f) : vec2(x / rhs, y / rhs); }
00084
00085
00086 friend vec2 operator*(float scaleFactor, const vec2 & rhs);
00087
00088 bool operator==(const vec2 & rhs) const;
00089 bool operator!=(const vec2 & rhs) const
00090 { return !((*this)==rhs); }
00091
00092
00093 void operator+=(const vec2 & rhs)
00094 { x+=rhs.x; y+=rhs.y;}
00095
00096 void operator-=(const vec2 & rhs)
00097 { x-=rhs.x; y-=rhs.y;}
00098
00099 void operator*=(const float rhs)
00100 { x*=rhs; y*=rhs; }
00101
00102 void operator/=(const float rhs)
00103 { if(rhs==0.0f)
00104 return;
00105 else
00106 { x/=rhs; y/=rhs; }
00107 }
00108
00109
00110
00111 vec2 operator-(void) const {return vec2(-x, -y);}
00112 vec2 operator+(void) const {return *this;}
00113
00114
00115 operator float* () const {return (float*) this;}
00116 operator const float* () const {return (const float*) this;}
00117
00118
00119 float x;
00120 float y;
00121 };
00122 }
00123
00124 #endif //__VEC2_H__