1 //-----------------------------------------------------------------------------
3 //-----------------------------------------------------------------------------
4 // GLVU : Copyright 1997 - 2002
5 // The University of North Carolina at Chapel Hill
6 //-----------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software
8 // and its documentation for any purpose is hereby granted without
9 // fee, provided that the above copyright notice appear in all copies
10 // and that both that copyright notice and this permission notice
11 // appear in supporting documentation. Binaries may be compiled with
12 // this software without any royalties or restrictions.
14 // The University of North Carolina at Chapel Hill makes no representations
15 // about the suitability of this software for any purpose. It is provided
16 // "as is" without express or implied warranty.
18 //==========================================================================
19 // vec2.hpp : 2d vector class template. Works for any integer or real type.
20 //==========================================================================
35 Vec2 (const Type X, const Type Y) { x=X; y=Y; };
36 Vec2 (const Vec2& v) { x=v.x; y=v.y; };
37 Vec2 (const Type v[2]) { x=v[0]; y=v[1]; };
38 void Set (const Type X, const Type Y) { x=X; y=Y; }
39 void Set (const Type v[2]) { x=v[0]; y=v[1]; };
41 operator Type*() // Type * CONVERSION
42 { return (Type *)&x; }
43 operator const Type*() const // CONST Type * CONVERSION
46 Vec2& operator = (const Vec2& A) // ASSIGNMENT (=)
50 bool operator == (const Vec2& A) const // COMPARISON (==)
51 { return (x==A.x && y==A.y); }
52 bool operator != (const Vec2& A) const // COMPARISON (!=)
53 { return (x!=A.x || y!=A.y); }
55 Vec2 operator + (const Vec2& A) const // ADDITION (+)
56 { Vec2 Sum(x+A.x, y+A.y);
58 Vec2 operator - (const Vec2& A) const // SUBTRACTION (-)
59 { Vec2 Diff(x-A.x, y-A.y);
61 Type operator * (const Vec2& A) const // DOT-PRODUCT (*)
62 { Type DotProd = x*A.x+y*A.y;
64 Type operator / (const Vec2& A) const // CROSS-PRODUCT (/)
65 { Type CrossProd = x*A.y-y*A.x;
67 Type operator ^ (const Vec2& A) const // ALSO CROSS-PRODUCT (^)
68 { Type CrossProd = x*A.y-y*A.x;
70 Vec2 operator * (const Type s) const // MULTIPLY BY SCALAR (*)
71 { Vec2 Scaled(x*s, y*s);
73 Vec2 operator / (const Type s) const // DIVIDE BY SCALAR (/)
74 { Vec2 Scaled(x/s, y/s);
76 Vec2 operator & (const Vec2& A) const // COMPONENT MULTIPLY (&)
77 { Vec2 CompMult(x*A.x, y*A.y);
80 friend inline Vec2 operator *(Type s, const Vec2& v) // SCALAR MULT s*V
81 { return Vec2(v.x*s, v.y*s); }
83 Vec2& operator += (const Vec2& A) // ACCUMULATED VECTOR ADDITION (+=)
84 { x+=A.x; y+=A.y; return *this; }
85 Vec2& operator -= (const Vec2& A) // ACCUMULATED VECTOR SUBTRACTION (-=)
86 { x-=A.x; y-=A.y; return *this; }
87 Vec2& operator *= (const Type s) // ACCUMULATED SCALAR MULT (*=)
88 { x*=s; y*=s; return *this; }
89 Vec2& operator /= (const Type s) // ACCUMULATED SCALAR DIV (/=)
90 { x/=s; y/=s; return *this; }
91 Vec2& operator &= (const Vec2& A) // ACCUMULATED COMPONENT MULTIPLY (&=)
92 { x*=A.x; y*=A.y; return *this; }
93 Vec2 operator - (void) const // NEGATION (-)
94 { Vec2 Negated(-x, -y);
98 const Type& operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
99 { return( (i==0)?x:y ); };
100 Type & operator [] (const int i)
101 { return( (i==0)?x:y ); };
104 Type Length (void) const // LENGTH OF VECTOR
105 { return ((Type)sqrt(x*x+y*y)); };
106 Type LengthSqr (void) const // LENGTH OF VECTOR (SQUARED)
107 { return (x*x+y*y); };
108 Vec2& Normalize (void) // NORMALIZE VECTOR
109 { Type L = Length(); // CALCULATE LENGTH
110 if (L>0) { x/=L; y/=L; }
112 }; // DIV COMPONENTS BY LENGTH
114 Vec2 Perpendicular() const // RETURNS A PERPENDICULAR
118 void UpdateMinMax(Vec2 &Min, Vec2 &Max)
120 if (x<Min.x) Min.x=x; else if (x>Max.x) Max.x=x;
121 if (y<Min.y) Min.y=y; else if (y>Max.y) Max.y=y;
125 { printf("(%.3f, %.3f)\n",x, y); }
130 typedef Vec2<float> Vec2f;
131 typedef Vec2<double> Vec2d;
133 template<class Type> Vec2<Type> Vec2<Type>::ZERO = Vec2<Type>(0,0);