1 /*************************************************************************
2  *                                                                       *
3  * Tokamak Physics Engine, Copyright (C) 2002-2007 David Lam.            *
4  * All rights reserved.  Email: david@tokamakphysics.com                 *
5  *                       Web: www.tokamakphysics.com                     *
6  *                                                                       *
7  * This library is distributed in the hope that it will be useful,       *
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
10  * LICENSE.TXT for more details.                                         *
11  *                                                                       *
12  *************************************************************************/
13 
14 ///////////////////////////////////////////////////////////////////////////
15 // VECTOR2 MEMBER FUNCTIONS
16 ///////////////////////////////////////////////////////////////////////////
17 
18 //=========================================================================
19 
neV2(void)20 NEINLINE neV2::neV2( void ){}
21 
22 //=========================================================================
23 
neV2(f32 x,f32 y)24 NEINLINE neV2::neV2( f32 x, f32 y )
25 {
26     X=x; Y=y;
27 }
28 
29 //=========================================================================
30 
neV2(const neV2 & V)31 NEINLINE neV2::neV2( const neV2& V )
32 {
33     *this = V;
34 }
35 
36 //==============================================================================
37 
neV2(const f32 & K)38 NEINLINE neV2::neV2( const f32& K )
39 {
40     X = K;//(f32)cos( R );
41     Y = K;//(f32)sin( R );
42 }
43 
44 //=========================================================================
45 
Zero(void)46 NEINLINE void neV2::Zero( void )
47 {
48     X=Y=0;
49 }
50 
51 //=========================================================================
52 
53 NEINLINE f32& neV2::operator[]( s32 I )
54 {
55     ASSERT( I >= 0 );
56     ASSERT( I <= 1 );
57     return ((f32*)(this))[I];
58 }
59 
60 //=========================================================================
61 
62 NEINLINE neV2& neV2::operator += ( const neV2& V )
63 {
64     X += V.X; Y += V.Y;
65     return *this;
66 }
67 
68 //=========================================================================
69 
70 NEINLINE neV2& neV2::operator -= ( const neV2& V )
71 {
72     X -= V.X; Y -= V.Y;
73     return *this;
74 }
75 
76 //=========================================================================
77 
78 NEINLINE neV2& neV2::operator /= ( f32 S )
79 {
80     *this = *this / S;
81     return *this;
82 }
83 
84 //=========================================================================
85 
86 NEINLINE neV2& neV2::operator *= ( f32 S )
87 {
88     *this = *this * S;
89     return *this;
90 }
91 
92 //=========================================================================
93 
Set(f32 x,f32 y)94 NEINLINE void neV2::Set( f32 x, f32 y )
95 {
96     X = x; Y = y;
97 }
98 
99 //=========================================================================
100 
Length(void)101 NEINLINE f32 neV2::Length( void ) const
102 {
103     return (f32)sqrt( this->Dot( *this ) );
104 }
105 
106 //=========================================================================
107 
Normalize(void)108 NEINLINE neV2& neV2::Normalize( void )
109 {
110     *this *= 1 / Length();
111     return *this;
112 }
113 
114 //=========================================================================
115 
Dot(const neV2 & V)116 NEINLINE f32 neV2::Dot( const neV2& V ) const
117 {
118     return X * V.X + Y * V.Y;
119 }
120 
121 //==============================================================================
122 
Cross(const neV2 & V)123 NEINLINE neV2 neV2::Cross( const neV2& V ) const
124 {
125     return neV2( (X * V.Y) - (Y * V.X) );
126 }
127 
128 //==============================================================================
129 
GetAngle(void)130 NEINLINE f32 neV2::GetAngle( void ) const
131 {
132     return (f32)atan2( Y, X );
133 }
134 
135 //==============================================================================
136 
Rotate(neRadian R)137 NEINLINE neV2& neV2::Rotate( neRadian R )
138 {
139     f32 s = (f32)sin( R );
140     f32 c = (f32)cos( R );
141     f32 x = X;
142     f32 y = Y;
143 
144     X  = (c * x) - (s * y);
145     Y  = (c * y) + (s * x);
146 
147     return *this;
148 }
149 
150 ///////////////////////////////////////////////////////////////////////////
151 // VECTOR2 FRIEND FUNCTIONS
152 ///////////////////////////////////////////////////////////////////////////
153 
154 //=========================================================================
155 
156 NEINLINE neV2 operator - ( const neV2& V )
157 {
158     return neV2( -V.X, -V.Y );
159 }
160 
161 //=========================================================================
162 
163 NEINLINE neV2 operator + ( const neV2& V1, const neV2& V2 )
164 {
165     return neV2( V1.X + V2.X, V1.Y + V2.Y  );
166 }
167 
168 //=========================================================================
169 
170 NEINLINE neV2 operator - ( const neV2& V1, const neV2& V2 )
171 {
172     return neV2( V1.X - V2.X, V1.Y - V2.Y  );
173 }
174 
175 //=========================================================================
176 
177 NEINLINE neV2 operator / ( const neV2& V, f32 S )
178 {
179     return V * (1/S);
180 }
181 
182 //=========================================================================
183 
184 NEINLINE neV2 operator * ( const neV2& V, const f32 S )
185 {
186     return neV2( V.X * S, V.Y * S );
187 }
188 
189 //=========================================================================
190 
191 NEINLINE neV2 operator * ( f32 S,  const neV2& V )
192 {
193     return V * S;
194 }
195 
196 //=========================================================================
197 
neV2_AngleBetween(const neV2 & V1,const neV2 & V2)198 NEINLINE neRadian neV2_AngleBetween( const neV2& V1, const neV2& V2 )
199 {
200     f32 D, C;
201 
202     D = V1.Length() * V2.Length();
203 
204     if( D == 0.0f ) return 0;
205 
206     C = V1.Dot( V2 ) / D;
207 
208     if     ( C >  1.0f )  C =  1.0f;
209     else if( C < -1.0f )  C = -1.0f;
210 
211     return (f32)acos( C );
212 }
213 
214 ///////////////////////////////////////////////////////////////////////////
215 // END
216 ///////////////////////////////////////////////////////////////////////////
217