source: GTP/trunk/Lib/Illum/GPUObscurancesGT/include/VECTOR2D.h @ 930

Revision 930, 3.0 KB checked in by igarcia, 18 years ago (diff)
Line 
1//////////////////////////////////////////////////////////////////////////////////////////
2//      VECTOR2D.h
3//      Class declaration for a 2d vector
4//      You may use this code however you wish, but if you do, please credit me and
5//      provide a link to my website in a readme file or similar
6//      Downloaded from: www.paulsprojects.net
7//      Created:        6th November 2002
8//      Modified:       7th January 2003        -       Added QuadraticInterpolate
9//////////////////////////////////////////////////////////////////////////////////////////     
10
11#ifndef VECTOR2D_H
12#define VECTOR2D_H
13
14class VECTOR2D
15{
16public:
17        //constructors
18        VECTOR2D(void)  :       x(0.0f), y(0.0f)
19        {}
20
21        VECTOR2D(float newX, float newY):       x(newX), y(newY)
22        {}
23
24        VECTOR2D(const float * rhs)             :       x(*rhs), y((*rhs)+1)
25        {}
26
27        VECTOR2D(const VECTOR2D & rhs)  :       x(rhs.x), y(rhs.y)
28        {}
29
30        ~VECTOR2D() {}  //empty
31
32        void Set(float newX, float newY)
33        {       x=newX; y=newY; }
34       
35        //Accessors kept for compatibility
36        void SetX(float newX) {x = newX;}
37        void SetY(float newY) {y = newY;}
38
39        float GetX() const {return x;}  //public accessor functions
40        float GetY() const {return y;}  //inline, const
41
42        void LoadZero(void);
43        void LoadOne(void);                                     //fill with (1, 1)
44       
45        void Normalize();
46        VECTOR2D GetNormalized() const;
47       
48        float GetLength() const
49        {       return (float)sqrt((x*x)+(y*y));        }
50       
51        float GetSquaredLength() const
52        {       return (x*x)+(y*y);     }
53
54        //linear interpolate
55        VECTOR2D lerp(const VECTOR2D & v2, float factor) const
56        {       return (*this)*(1.0f-factor) + v2*factor;       }
57
58        VECTOR2D QuadraticInterpolate(const VECTOR2D & v2, const VECTOR2D & v3, float factor) const
59        {       return (*this)*(1.0f-factor)*(1.0f-factor) + 2*v2*factor*(1.0f-factor) + v3*factor*factor;}
60
61        //overloaded operators
62        //binary operators
63        VECTOR2D operator+(const VECTOR2D & rhs) const
64        {       return VECTOR2D(x + rhs.x, y + rhs.y);  }
65
66        VECTOR2D operator-(const VECTOR2D & rhs) const
67        {       return VECTOR2D(x - rhs.x, y - rhs.y);  }
68
69        VECTOR2D operator*(const float rhs) const
70        {       return VECTOR2D(x*rhs, y*rhs);  }
71       
72        VECTOR2D operator/(const float rhs) const
73        {       return (rhs==0) ? VECTOR2D(0.0f, 0.0f) : VECTOR2D(x / rhs, y / rhs);    }
74
75        //multiply by a float, eg 3*v
76        friend VECTOR2D operator*(float scaleFactor, const VECTOR2D & rhs);
77
78        bool operator==(const VECTOR2D & rhs) const;
79        bool operator!=(const VECTOR2D & rhs) const
80        {       return !((*this)==rhs); }
81
82        //self-add etc
83        void operator+=(const VECTOR2D & rhs)
84        {       x+=rhs.x;       y+=rhs.y;}
85
86        void operator-=(const VECTOR2D & rhs)
87        {       x-=rhs.x;       y-=rhs.y;}
88
89        void operator*=(const float rhs)
90        {       x*=rhs; y*=rhs; }
91
92        void operator/=(const float rhs)
93        {       if(rhs==0.0f)
94                        return;
95                else
96                {       x/=rhs; y/=rhs; }
97        }
98
99
100        //unary operators
101        VECTOR2D operator-(void) const {return VECTOR2D(-x, -y);}
102        VECTOR2D operator+(void) const {return *this;}
103
104        //cast to pointer to a (float *) for glVertex3fv etc
105        operator float* () const {return (float*) this;}
106        operator const float* () const {return (const float*) this;}
107
108        //member variables
109        float x;
110        float y;
111};
112
113#endif  //VECTOR2D_H
Note: See TracBrowser for help on using the repository browser.