00001
00002
00003
00004
00005 #ifndef VEC2_H
00006 #define VEC2_H
00007
00008 #include <stdio.h>
00009 #include <math.h>
00010
00011 template<class Type>
00012 class Vec2
00013 {
00014 public:
00015 Type x, y;
00016
00017 Vec2 (void) {};
00018 Vec2 (const Type X, const Type Y) { x=X; y=Y; };
00019 Vec2 (const Vec2& v) { x=v.x; y=v.y; };
00020 Vec2 (const Type v[2]) { x=v[0]; y=v[1]; };
00021 void Set (const Type X, const Type Y) { x=X; y=Y; }
00022 void Set (const Type v[2]) { x=v[0]; y=v[1]; };
00023
00024 operator Type*()
00025 { return (Type *)&x; }
00026 operator const Type*() const
00027 { return &x; }
00028
00029 Vec2& operator = (const Vec2& A)
00030 { x=A.x; y=A.y;
00031 return(*this); };
00032
00033 int operator == (const Vec2& A) const
00034 { return (x==A.x && y==A.y); }
00035
00036 Vec2 operator + (const Vec2& A) const
00037 { Vec2 Sum(x+A.x, y+A.y);
00038 return(Sum); }
00039 Vec2 operator - (const Vec2& A) const
00040 { Vec2 Diff(x-A.x, y-A.y);
00041 return(Diff); }
00042 Type operator * (const Vec2& A) const
00043 { Type DotProd = x*A.x+y*A.y;
00044 return(DotProd); }
00045 Type operator / (const Vec2& A) const
00046 { Type CrossProd = x*A.y-y*A.x;
00047 return(CrossProd); }
00048 Type operator ^ (const Vec2& A) const
00049 { Type CrossProd = x*A.y-y*A.x;
00050 return(CrossProd); }
00051 Vec2 operator * (const Type s) const
00052 { Vec2 Scaled(x*s, y*s);
00053 return(Scaled); }
00054 Vec2 operator / (const Type s) const
00055 { Vec2 Scaled(x/s, y/s);
00056 return(Scaled); }
00057 Vec2 operator & (const Vec2& A) const
00058 { Vec2 CompMult(x*A.x, y*A.y);
00059 return(CompMult); }
00060
00061 friend inline Vec2 operator *(Type s, const Vec2& v)
00062 { return Vec2(v.x*s, v.y*s); }
00063
00064 Vec2& operator += (const Vec2& A)
00065 { x+=A.x; y+=A.y; return *this; }
00066 Vec2& operator -= (const Vec2& A)
00067 { x-=A.x; y-=A.y; return *this; }
00068 Vec2& operator *= (const Type s)
00069 { x*=s; y*=s; return *this; }
00070 Vec2& operator /= (const Type s)
00071 { x/=s; y/=s; return *this; }
00072 Vec2& operator &= (const Vec2& A)
00073 { x*=A.x; y*=A.y; return *this; }
00074 Vec2 operator - (void) const
00075 { Vec2 Negated(-x, -y);
00076 return(Negated); };
00077
00078
00079
00080
00081
00082
00083
00084
00085 Type Length (void) const
00086 { return ((Type)sqrt(x*x+y*y)); };
00087 Type LengthSqr (void) const
00088 { return (x*x+y*y); };
00089 Vec2& Normalize (void)
00090 { Type L = Length();
00091 if (L>0) { x/=L; y/=L; }
00092 return *this;
00093 };
00094
00095 Vec2 Perpendicular() const
00096 { Vec2 Perp(-y,x);
00097 return(Perp); }
00098
00099 void UpdateMinMax(Vec2 &Min, Vec2 &Max)
00100 {
00101 if (x<Min.x) Min.x=x; else if (x>Max.x) Max.x=x;
00102 if (y<Min.y) Min.y=y; else if (y>Max.y) Max.y=y;
00103 }
00104
00105 void Print() const
00106 { printf("(%.3f, %.3f)\n",x, y); }
00107
00108 static Vec2 ZERO;
00109 };
00110
00111 typedef Vec2<float> Vec2f;
00112 typedef Vec2<double> Vec2d;
00113
00114 template<class Type> Vec2<Type> Vec2<Type>::ZERO = Vec2<Type>(0,0);
00115
00116 #endif
00117
00118