Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

vec2f.hpp

00001 //==========================================================================
00002 // vec2.hpp : 2d vector class template. Works for any integer or real type.
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*()                            // Type * CONVERSION
00025       { return (Type *)&x; }
00026     operator const Type*() const                // CONST Type * CONVERSION
00027       { return &x; }
00028 
00029     Vec2& operator = (const Vec2& A)            // ASSIGNMENT (=)
00030       { x=A.x; y=A.y;
00031         return(*this);  };
00032 
00033     int operator == (const Vec2& A) const       // COMPARISON (==)
00034       { return (x==A.x && y==A.y); }
00035 
00036     Vec2 operator + (const Vec2& A) const       // ADDITION (+)
00037       { Vec2 Sum(x+A.x, y+A.y); 
00038         return(Sum); }
00039     Vec2 operator - (const Vec2& A) const       // SUBTRACTION (-)
00040       { Vec2 Diff(x-A.x, y-A.y);
00041         return(Diff); }
00042     Type operator * (const Vec2& A) const       // DOT-PRODUCT (*)
00043       { Type DotProd = x*A.x+y*A.y; 
00044         return(DotProd); }
00045     Type operator / (const Vec2& A) const       // CROSS-PRODUCT (/)
00046       { Type CrossProd = x*A.y-y*A.x;
00047         return(CrossProd); }
00048     Type operator ^ (const Vec2& A) const       // ALSO CROSS-PRODUCT (^)
00049       { Type CrossProd = x*A.y-y*A.x;
00050         return(CrossProd); }
00051     Vec2 operator * (const Type s) const        // MULTIPLY BY SCALAR (*)
00052       { Vec2 Scaled(x*s, y*s); 
00053         return(Scaled); }
00054     Vec2 operator / (const Type s) const        // DIVIDE BY SCALAR (/)
00055       { Vec2 Scaled(x/s, y/s);
00056         return(Scaled); }
00057     Vec2 operator & (const Vec2& A) const       // COMPONENT MULTIPLY (&)
00058       { Vec2 CompMult(x*A.x, y*A.y);
00059         return(CompMult); }
00060 
00061     friend inline Vec2 operator *(Type s, const Vec2& v)  // SCALAR MULT s*V
00062       { return Vec2(v.x*s, v.y*s); }
00063 
00064     Vec2& operator += (const Vec2& A)  // ACCUMULATED VECTOR ADDITION (+=)
00065       { x+=A.x; y+=A.y; return *this; }
00066     Vec2& operator -= (const Vec2& A)  // ACCUMULATED VECTOR SUBTRACTION (-=)
00067       { x-=A.x; y-=A.y; return *this; }
00068     Vec2& operator *= (const Type s)   // ACCUMULATED SCALAR MULT (*=)
00069       { x*=s; y*=s; return *this; }
00070     Vec2& operator /= (const Type s)   // ACCUMULATED SCALAR DIV (/=)
00071       { x/=s; y/=s; return *this; }
00072     Vec2& operator &= (const Vec2& A)  // ACCUMULATED COMPONENT MULTIPLY (&=)
00073       { x*=A.x; y*=A.y; return *this; }
00074     Vec2 operator - (void) const      // NEGATION (-)
00075       { Vec2 Negated(-x, -y);
00076         return(Negated); };
00077 
00078 /*
00079     const Type& operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
00080       { return( (i==0)?x:y ); };
00081     Type & operator [] (const int i)
00082       { return( (i==0)?x:y ); };
00083 */
00084 
00085     Type Length (void) const                     // LENGTH OF VECTOR
00086       { return ((Type)sqrt(x*x+y*y)); };
00087     Type LengthSqr (void) const                  // LENGTH OF VECTOR (SQUARED)
00088       { return (x*x+y*y); };
00089     Vec2& Normalize (void)                       // NORMALIZE VECTOR
00090       { Type L = Length();                       // CALCULATE LENGTH
00091         if (L>0) { x/=L; y/=L; }
00092         return *this;
00093       };                                          // DIV COMPONENTS BY LENGTH
00094 
00095     Vec2 Perpendicular() const                    // RETURNS A PERPENDICULAR
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 

Generated at Fri Oct 12 15:12:21 2001 for GLVU by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001