]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/vec2f.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / vec2f.hpp
1 //-----------------------------------------------------------------------------
2 // File : vec2f.hpp
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.
13 //
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.
17
18 //==========================================================================
19 // vec2.hpp : 2d vector class template. Works for any integer or real type.
20 //==========================================================================
21
22 #ifndef VEC2_H
23 #define VEC2_H
24
25 #include <stdio.h>
26 #include <math.h>
27
28 template<class Type>
29 class Vec2
30 {
31   public:
32     Type x, y;
33
34     Vec2 (void) {};
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]; };
40
41     operator Type*()                            // Type * CONVERSION
42       { return (Type *)&x; }
43     operator const Type*() const                // CONST Type * CONVERSION
44       { return &x; }
45
46     Vec2& operator = (const Vec2& A)            // ASSIGNMENT (=)
47       { x=A.x; y=A.y;
48         return(*this);  };
49
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); }
54
55     Vec2 operator + (const Vec2& A) const       // ADDITION (+)
56       { Vec2 Sum(x+A.x, y+A.y); 
57         return(Sum); }
58     Vec2 operator - (const Vec2& A) const       // SUBTRACTION (-)
59       { Vec2 Diff(x-A.x, y-A.y);
60         return(Diff); }
61     Type operator * (const Vec2& A) const       // DOT-PRODUCT (*)
62       { Type DotProd = x*A.x+y*A.y; 
63         return(DotProd); }
64     Type operator / (const Vec2& A) const       // CROSS-PRODUCT (/)
65       { Type CrossProd = x*A.y-y*A.x;
66         return(CrossProd); }
67     Type operator ^ (const Vec2& A) const       // ALSO CROSS-PRODUCT (^)
68       { Type CrossProd = x*A.y-y*A.x;
69         return(CrossProd); }
70     Vec2 operator * (const Type s) const        // MULTIPLY BY SCALAR (*)
71       { Vec2 Scaled(x*s, y*s); 
72         return(Scaled); }
73     Vec2 operator / (const Type s) const        // DIVIDE BY SCALAR (/)
74       { Vec2 Scaled(x/s, y/s);
75         return(Scaled); }
76     Vec2 operator & (const Vec2& A) const       // COMPONENT MULTIPLY (&)
77       { Vec2 CompMult(x*A.x, y*A.y);
78         return(CompMult); }
79
80     friend inline Vec2 operator *(Type s, const Vec2& v)  // SCALAR MULT s*V
81       { return Vec2(v.x*s, v.y*s); }
82
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);
95         return(Negated); };
96
97 /*
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 ); };
102 */
103
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; }
111         return *this;
112       };                                          // DIV COMPONENTS BY LENGTH
113
114     Vec2 Perpendicular() const                    // RETURNS A PERPENDICULAR
115       { Vec2 Perp(-y,x);
116         return(Perp); }
117
118     void UpdateMinMax(Vec2 &Min, Vec2 &Max)
119     {
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;
122     }
123
124     void Print() const
125       { printf("(%.3f, %.3f)\n",x, y); }
126
127     static Vec2 ZERO;
128 };
129
130 typedef Vec2<float> Vec2f;
131 typedef Vec2<double> Vec2d;
132
133 template<class Type> Vec2<Type> Vec2<Type>::ZERO = Vec2<Type>(0,0);
134
135 #endif
136
137