Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Sun Sep 23 10:11:43 2012 +0000
Revision:
31:5f039cbddee8
Parent:
30:d8af03f01cd4
Child:
40:3ba2b0ea9f33
this is quite nice, but  I am going to make a deep modification of the bouncing principle: instead of depending on the penetration, it will just be a factor over the speed (perfect elastic bouncing being factorElastic=1, and perfectly absorption=0);

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedalvaro 31:5f039cbddee8 1 // class vector2D (for the time being, only 2d):
mbedalvaro 31:5f039cbddee8 2
mbedalvaro 31:5f039cbddee8 3 #include "mbed.h"
mbedalvaro 31:5f039cbddee8 4
mbedalvaro 31:5f039cbddee8 5 #ifndef vector2D_H
mbedalvaro 31:5f039cbddee8 6 #define vector2D_H
mbedalvaro 31:5f039cbddee8 7
mbedalvaro 31:5f039cbddee8 8 #ifndef DEG_TO_RAD
mbedalvaro 31:5f039cbddee8 9 #define DEG_TO_RAD (PI/180.0)
mbedalvaro 31:5f039cbddee8 10 #endif
mbedalvaro 31:5f039cbddee8 11
mbedalvaro 31:5f039cbddee8 12 #ifndef RAD_TO_DEG
mbedalvaro 31:5f039cbddee8 13 #define RAD_TO_DEG (180.0/PI)
mbedalvaro 31:5f039cbddee8 14 #endif
mbedalvaro 31:5f039cbddee8 15
mbedalvaro 31:5f039cbddee8 16 #ifndef CW
mbedalvaro 31:5f039cbddee8 17 #define CW 1.0
mbedalvaro 31:5f039cbddee8 18 #endif
mbedalvaro 31:5f039cbddee8 19
mbedalvaro 31:5f039cbddee8 20 #ifndef CCW
mbedalvaro 31:5f039cbddee8 21 #define CCW -1.0
mbedalvaro 31:5f039cbddee8 22 #endif
mbedalvaro 31:5f039cbddee8 23
mbedalvaro 31:5f039cbddee8 24 #ifndef PI
mbedalvaro 31:5f039cbddee8 25 #define PI 3.14159265889
mbedalvaro 31:5f039cbddee8 26 #endif
mbedalvaro 31:5f039cbddee8 27
mbedalvaro 31:5f039cbddee8 28 template <class T>
mbedalvaro 31:5f039cbddee8 29 class vector2D {
mbedalvaro 31:5f039cbddee8 30
mbedalvaro 31:5f039cbddee8 31 public:
mbedalvaro 31:5f039cbddee8 32
mbedalvaro 31:5f039cbddee8 33 // Overloaded constructor with parameters:
mbedalvaro 31:5f039cbddee8 34 vector2D( T _x=0.0f, T _y=0.0f );
mbedalvaro 31:5f039cbddee8 35 // note: we use the default copy constructor
mbedalvaro 31:5f039cbddee8 36
mbedalvaro 31:5f039cbddee8 37 // Explicit setting:
mbedalvaro 31:5f039cbddee8 38 void set( T _x, T _y );
mbedalvaro 31:5f039cbddee8 39 void set( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 40
mbedalvaro 31:5f039cbddee8 41 // Comparison:
mbedalvaro 31:5f039cbddee8 42 bool operator==( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 43 bool operator!=( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 44 bool match( const vector2D& vec, float tollerance=0.0001 );
mbedalvaro 31:5f039cbddee8 45
mbedalvaro 31:5f039cbddee8 46 // Overloaded operators:
mbedalvaro 31:5f039cbddee8 47 //
mbedalvaro 31:5f039cbddee8 48 void operator=( const vector2D& vec ); // I cannot declare this if we want also operator chaining?
mbedalvaro 31:5f039cbddee8 49 //vector2D & operator=( const vector2D& vec ); // this is to enable operator chaining (vec1=vec2=vec3).
mbedalvaro 31:5f039cbddee8 50 vector2D operator+( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 51 vector2D& operator+=( const vector2D& vec ); // why it has an output? for doing vec1=vec2+=vec3? YES!!! (operator chaining).
mbedalvaro 31:5f039cbddee8 52 vector2D operator-( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 53 vector2D& operator-=( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 54 vector2D operator*( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 55 vector2D& operator*=( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 56 vector2D operator/( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 57 vector2D& operator/=( const vector2D& vec );
mbedalvaro 31:5f039cbddee8 58
mbedalvaro 31:5f039cbddee8 59 //operator overloading for float:
mbedalvaro 31:5f039cbddee8 60 void operator=( const T f); // I cannot declare this if we want also operator chaining?
mbedalvaro 31:5f039cbddee8 61 //vector2D & operator=( const float& val ); // to allow operator chaining
mbedalvaro 31:5f039cbddee8 62 vector2D operator+( const T f ) const;
mbedalvaro 31:5f039cbddee8 63 vector2D& operator+=( const T f );
mbedalvaro 31:5f039cbddee8 64 vector2D operator-( const T f ) const;
mbedalvaro 31:5f039cbddee8 65 vector2D& operator-=( const T f );
mbedalvaro 31:5f039cbddee8 66 vector2D operator-() const;
mbedalvaro 31:5f039cbddee8 67
mbedalvaro 31:5f039cbddee8 68
mbedalvaro 31:5f039cbddee8 69 // multiplication by a scalar:
mbedalvaro 31:5f039cbddee8 70 vector2D operator*( const T f ) const;
mbedalvaro 31:5f039cbddee8 71 vector2D& operator*=( const T f );
mbedalvaro 31:5f039cbddee8 72 vector2D operator/( const T f ) const;
mbedalvaro 31:5f039cbddee8 73 vector2D& operator/=( const T f );
mbedalvaro 31:5f039cbddee8 74
mbedalvaro 31:5f039cbddee8 75 // Distance (between end points of two vector2Ds):
mbedalvaro 31:5f039cbddee8 76 float distance( const vector2D& pnt) const;
mbedalvaro 31:5f039cbddee8 77 float squareDistance( const vector2D& pnt ) const;
mbedalvaro 31:5f039cbddee8 78
mbedalvaro 31:5f039cbddee8 79 // Length of vector2D (norm):
mbedalvaro 31:5f039cbddee8 80 float length() const;
mbedalvaro 31:5f039cbddee8 81 float squareLength() const; // faster, no sqrt
mbedalvaro 31:5f039cbddee8 82
mbedalvaro 31:5f039cbddee8 83 // Scaling:
mbedalvaro 31:5f039cbddee8 84 vector2D getScaled( const float length ) const;
mbedalvaro 31:5f039cbddee8 85 vector2D& scale( const float length );
mbedalvaro 31:5f039cbddee8 86
mbedalvaro 31:5f039cbddee8 87 // Normalization:
mbedalvaro 31:5f039cbddee8 88 vector2D getNormalized() const;
mbedalvaro 31:5f039cbddee8 89 vector2D& normalize();
mbedalvaro 31:5f039cbddee8 90
mbedalvaro 31:5f039cbddee8 91 // Perpendicular normalized vector2D.
mbedalvaro 31:5f039cbddee8 92 vector2D getPerpendicularNormed(int orientation) const;
mbedalvaro 31:5f039cbddee8 93 vector2D& perpendicular(int orientation);
mbedalvaro 31:5f039cbddee8 94
mbedalvaro 31:5f039cbddee8 95 // Rotation
mbedalvaro 31:5f039cbddee8 96 vector2D getRotatedDeg( float angle ) const;
mbedalvaro 31:5f039cbddee8 97 vector2D getRotatedRad( float angle ) const;
mbedalvaro 31:5f039cbddee8 98 vector2D& rotateDeg( float angle );
mbedalvaro 31:5f039cbddee8 99 vector2D& rotateRad( float angle );
mbedalvaro 31:5f039cbddee8 100
mbedalvaro 31:5f039cbddee8 101 //vector2D product (for 3d vector2Ds - for 2d vector2Ds, something like this is just the "angle" between them):
mbedalvaro 31:5f039cbddee8 102 //vector2D getvector2DProduct(const vector2D& vec) const;
mbedalvaro 31:5f039cbddee8 103 //vector2D& vector2DProduct(const vector2D& vec) const;
mbedalvaro 31:5f039cbddee8 104
mbedalvaro 31:5f039cbddee8 105 //Angle (deg) between two vector2Ds (using atan2, so between -180 and 180)
mbedalvaro 31:5f039cbddee8 106 float angleDeg( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 107 float angleRad( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 108 float angleDegHoriz( ) const; // particular case when the second vector is just (1,0)
mbedalvaro 31:5f039cbddee8 109
mbedalvaro 31:5f039cbddee8 110 //Dot Product:
mbedalvaro 31:5f039cbddee8 111 float dot( const vector2D& vec ) const;
mbedalvaro 31:5f039cbddee8 112
mbedalvaro 31:5f039cbddee8 113 // =================================================================
mbedalvaro 31:5f039cbddee8 114
mbedalvaro 31:5f039cbddee8 115 // Actual variables:
mbedalvaro 31:5f039cbddee8 116 T x, y; // or make a class "point"
mbedalvaro 31:5f039cbddee8 117
mbedalvaro 31:5f039cbddee8 118 };
mbedalvaro 31:5f039cbddee8 119
mbedalvaro 31:5f039cbddee8 120 /////////////////
mbedalvaro 31:5f039cbddee8 121 // Implementation
mbedalvaro 31:5f039cbddee8 122 /////////////////
mbedalvaro 31:5f039cbddee8 123
mbedalvaro 31:5f039cbddee8 124 template <class T>
mbedalvaro 31:5f039cbddee8 125 inline vector2D<T>::vector2D( T _x, T _y ) {
mbedalvaro 31:5f039cbddee8 126 x = _x; y = _y;
mbedalvaro 31:5f039cbddee8 127 }
mbedalvaro 31:5f039cbddee8 128
mbedalvaro 31:5f039cbddee8 129 template <class T>
mbedalvaro 31:5f039cbddee8 130 inline void vector2D<T>::set( T _x, T _y ) {
mbedalvaro 31:5f039cbddee8 131 x = _x; y = _y;
mbedalvaro 31:5f039cbddee8 132 }
mbedalvaro 31:5f039cbddee8 133
mbedalvaro 31:5f039cbddee8 134 template <class T>
mbedalvaro 31:5f039cbddee8 135 inline void vector2D<T>::set( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 136 x=vec.x; y=vec.y;
mbedalvaro 31:5f039cbddee8 137 }
mbedalvaro 31:5f039cbddee8 138
mbedalvaro 31:5f039cbddee8 139 template <class T>
mbedalvaro 31:5f039cbddee8 140 inline bool vector2D<T>::operator==( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 141 return (x == vec.x) && (y == vec.y);
mbedalvaro 31:5f039cbddee8 142 }
mbedalvaro 31:5f039cbddee8 143
mbedalvaro 31:5f039cbddee8 144 template <class T>
mbedalvaro 31:5f039cbddee8 145 inline bool vector2D<T>::operator!=( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 146 return (x != vec.x) || (y != vec.y);
mbedalvaro 31:5f039cbddee8 147 }
mbedalvaro 31:5f039cbddee8 148
mbedalvaro 31:5f039cbddee8 149 template <class T>
mbedalvaro 31:5f039cbddee8 150 inline bool vector2D<T>::match( const vector2D<T>& vec, float tolerance ) {
mbedalvaro 31:5f039cbddee8 151 return (abs(x - vec.x) < tolerance)&& (abs(y - vec.y) < tolerance);
mbedalvaro 31:5f039cbddee8 152 }
mbedalvaro 31:5f039cbddee8 153
mbedalvaro 31:5f039cbddee8 154
mbedalvaro 31:5f039cbddee8 155 /*
mbedalvaro 31:5f039cbddee8 156 inline vector2D & operator=( const vector2D& vec ){ // returning a reference to the vector2D object for allowing operator chaining
mbedalvaro 31:5f039cbddee8 157 x = vec.x;
mbedalvaro 31:5f039cbddee8 158 y = vec.y;
mbedalvaro 31:5f039cbddee8 159 return *this;
mbedalvaro 31:5f039cbddee8 160 }
mbedalvaro 31:5f039cbddee8 161 */
mbedalvaro 31:5f039cbddee8 162
mbedalvaro 31:5f039cbddee8 163 template <class T>
mbedalvaro 31:5f039cbddee8 164 inline void vector2D<T>::operator=( const vector2D<T>& vec ){
mbedalvaro 31:5f039cbddee8 165 x = vec.x; y = vec.y;
mbedalvaro 31:5f039cbddee8 166 }
mbedalvaro 31:5f039cbddee8 167
mbedalvaro 31:5f039cbddee8 168 template <class T>
mbedalvaro 31:5f039cbddee8 169 inline vector2D<T> vector2D<T>::operator+( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 170 return vector2D<T>( x+vec.x, y+vec.y);
mbedalvaro 31:5f039cbddee8 171 }
mbedalvaro 31:5f039cbddee8 172
mbedalvaro 31:5f039cbddee8 173 template <class T>
mbedalvaro 31:5f039cbddee8 174 inline vector2D<T>& vector2D<T>::operator+=( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 175 x += vec.x;
mbedalvaro 31:5f039cbddee8 176 y += vec.y;
mbedalvaro 31:5f039cbddee8 177 return *this;
mbedalvaro 31:5f039cbddee8 178 }
mbedalvaro 31:5f039cbddee8 179
mbedalvaro 31:5f039cbddee8 180 template <class T>
mbedalvaro 31:5f039cbddee8 181 inline vector2D<T> vector2D<T>::operator-( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 182 return vector2D<T>(x-vec.x, y-vec.y);
mbedalvaro 31:5f039cbddee8 183 }
mbedalvaro 31:5f039cbddee8 184
mbedalvaro 31:5f039cbddee8 185 template <class T>
mbedalvaro 31:5f039cbddee8 186 inline vector2D<T>& vector2D<T>::operator-=( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 187 x -= vec.x;
mbedalvaro 31:5f039cbddee8 188 y -= vec.y;
mbedalvaro 31:5f039cbddee8 189 return *this;
mbedalvaro 31:5f039cbddee8 190 }
mbedalvaro 31:5f039cbddee8 191
mbedalvaro 31:5f039cbddee8 192 template <class T>
mbedalvaro 31:5f039cbddee8 193 inline vector2D<T> vector2D<T>::operator*( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 194 return vector2D<T>(x*vec.x, y*vec.y);
mbedalvaro 31:5f039cbddee8 195 }
mbedalvaro 31:5f039cbddee8 196
mbedalvaro 31:5f039cbddee8 197 template <class T>
mbedalvaro 31:5f039cbddee8 198 inline vector2D<T>& vector2D<T>::operator*=( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 199 x*=vec.x;
mbedalvaro 31:5f039cbddee8 200 y*=vec.y;
mbedalvaro 31:5f039cbddee8 201 return *this;
mbedalvaro 31:5f039cbddee8 202 }
mbedalvaro 31:5f039cbddee8 203
mbedalvaro 31:5f039cbddee8 204 template <class T>
mbedalvaro 31:5f039cbddee8 205 inline vector2D<T> vector2D<T>::operator/( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 206 return vector2D<T>( vec.x!=0 ? x/vec.x : x , vec.y!=0 ? y/vec.y : y);
mbedalvaro 31:5f039cbddee8 207 }
mbedalvaro 31:5f039cbddee8 208
mbedalvaro 31:5f039cbddee8 209 template <class T>
mbedalvaro 31:5f039cbddee8 210 inline vector2D<T>& vector2D<T>::operator/=( const vector2D<T>& vec ) {
mbedalvaro 31:5f039cbddee8 211 vec.x!=0 ? x/=vec.x : x;
mbedalvaro 31:5f039cbddee8 212 vec.y!=0 ? y/=vec.y : y;
mbedalvaro 31:5f039cbddee8 213 return *this;
mbedalvaro 31:5f039cbddee8 214 }
mbedalvaro 31:5f039cbddee8 215
mbedalvaro 31:5f039cbddee8 216 //operator overloading for float:
mbedalvaro 31:5f039cbddee8 217 /*
mbedalvaro 31:5f039cbddee8 218 inline vector2D<T> & operator=( const float& val ){
mbedalvaro 31:5f039cbddee8 219 x = val;
mbedalvaro 31:5f039cbddee8 220 y = val;
mbedalvaro 31:5f039cbddee8 221 return *this;
mbedalvaro 31:5f039cbddee8 222 }
mbedalvaro 31:5f039cbddee8 223 */
mbedalvaro 31:5f039cbddee8 224
mbedalvaro 31:5f039cbddee8 225 template <class T>
mbedalvaro 31:5f039cbddee8 226 inline void vector2D<T>::operator=( const T f){
mbedalvaro 31:5f039cbddee8 227 x = f; y = f;
mbedalvaro 31:5f039cbddee8 228 }
mbedalvaro 31:5f039cbddee8 229
mbedalvaro 31:5f039cbddee8 230 template <class T>
mbedalvaro 31:5f039cbddee8 231 inline vector2D<T> vector2D<T>::operator+( const T f ) const {
mbedalvaro 31:5f039cbddee8 232 return vector2D<T>( x+f, y+f);
mbedalvaro 31:5f039cbddee8 233 }
mbedalvaro 31:5f039cbddee8 234
mbedalvaro 31:5f039cbddee8 235 template <class T>
mbedalvaro 31:5f039cbddee8 236 inline vector2D<T>& vector2D<T>::operator+=( const T f ) {
mbedalvaro 31:5f039cbddee8 237 x += f; y += f;
mbedalvaro 31:5f039cbddee8 238 return *this;
mbedalvaro 31:5f039cbddee8 239 }
mbedalvaro 31:5f039cbddee8 240
mbedalvaro 31:5f039cbddee8 241 template <class T>
mbedalvaro 31:5f039cbddee8 242 inline vector2D<T> vector2D<T>::operator-( const T f ) const {
mbedalvaro 31:5f039cbddee8 243 return vector2D<T>( x-f, y-f);
mbedalvaro 31:5f039cbddee8 244 }
mbedalvaro 31:5f039cbddee8 245
mbedalvaro 31:5f039cbddee8 246 template <class T>
mbedalvaro 31:5f039cbddee8 247 inline vector2D<T>& vector2D<T>::operator-=( const T f ) {
mbedalvaro 31:5f039cbddee8 248 x -= f; y -= f;
mbedalvaro 31:5f039cbddee8 249 return *this;
mbedalvaro 31:5f039cbddee8 250 }
mbedalvaro 31:5f039cbddee8 251
mbedalvaro 31:5f039cbddee8 252 template <class T>
mbedalvaro 31:5f039cbddee8 253 inline vector2D<T> vector2D<T>::operator-() const {
mbedalvaro 31:5f039cbddee8 254 return vector2D<T>(-x, -y);
mbedalvaro 31:5f039cbddee8 255 }
mbedalvaro 31:5f039cbddee8 256
mbedalvaro 31:5f039cbddee8 257 template <class T>
mbedalvaro 31:5f039cbddee8 258 inline vector2D<T> vector2D<T>::operator*( const T f ) const {
mbedalvaro 31:5f039cbddee8 259 return vector2D<T>(x*f, y*f);
mbedalvaro 31:5f039cbddee8 260 }
mbedalvaro 31:5f039cbddee8 261
mbedalvaro 31:5f039cbddee8 262 template <class T>
mbedalvaro 31:5f039cbddee8 263 inline vector2D<T>& vector2D<T>::operator*=( const T f ) {
mbedalvaro 31:5f039cbddee8 264 x*=f; y*=f;
mbedalvaro 31:5f039cbddee8 265 return *this;
mbedalvaro 31:5f039cbddee8 266 }
mbedalvaro 31:5f039cbddee8 267
mbedalvaro 31:5f039cbddee8 268 template <class T>
mbedalvaro 31:5f039cbddee8 269 inline vector2D<T> vector2D<T>::operator/( const T f ) const {
mbedalvaro 31:5f039cbddee8 270 //cout << "here" << endl;
mbedalvaro 31:5f039cbddee8 271 if(f == 0) return vector2D<T>(x, y);
mbedalvaro 31:5f039cbddee8 272 return vector2D<T>(x/f, y/f);
mbedalvaro 31:5f039cbddee8 273 }
mbedalvaro 31:5f039cbddee8 274
mbedalvaro 31:5f039cbddee8 275 template <class T>
mbedalvaro 31:5f039cbddee8 276 inline vector2D<T>& vector2D<T>::operator/=( const T f ) {
mbedalvaro 31:5f039cbddee8 277 if(f == 0) return *this;
mbedalvaro 31:5f039cbddee8 278 x/=f; y/=f;
mbedalvaro 31:5f039cbddee8 279 return *this;
mbedalvaro 31:5f039cbddee8 280 }
mbedalvaro 31:5f039cbddee8 281
mbedalvaro 31:5f039cbddee8 282 template <class T>
mbedalvaro 31:5f039cbddee8 283 inline vector2D<T> vector2D<T>::getScaled( const float length ) const {
mbedalvaro 31:5f039cbddee8 284 float l = (float)sqrt(x*x + y*y);
mbedalvaro 31:5f039cbddee8 285 if( l > 0 )
mbedalvaro 31:5f039cbddee8 286 return vector2D<T>( (x/l)*length, (y/l)*length );
mbedalvaro 31:5f039cbddee8 287 else
mbedalvaro 31:5f039cbddee8 288 return vector2D<T>();
mbedalvaro 31:5f039cbddee8 289 }
mbedalvaro 31:5f039cbddee8 290
mbedalvaro 31:5f039cbddee8 291 template <class T>
mbedalvaro 31:5f039cbddee8 292 inline vector2D<T>& vector2D<T>::scale( const float length ) {
mbedalvaro 31:5f039cbddee8 293 float l = (float)sqrt(x*x + y*y);
mbedalvaro 31:5f039cbddee8 294 if (l > 0) {
mbedalvaro 31:5f039cbddee8 295 x = (x/l)*length;
mbedalvaro 31:5f039cbddee8 296 y = (y/l)*length;
mbedalvaro 31:5f039cbddee8 297 }
mbedalvaro 31:5f039cbddee8 298 return *this;
mbedalvaro 31:5f039cbddee8 299 }
mbedalvaro 31:5f039cbddee8 300
mbedalvaro 31:5f039cbddee8 301 // Rotation
mbedalvaro 31:5f039cbddee8 302 //
mbedalvaro 31:5f039cbddee8 303 //
mbedalvaro 31:5f039cbddee8 304
mbedalvaro 31:5f039cbddee8 305 template <class T>
mbedalvaro 31:5f039cbddee8 306 inline vector2D<T> vector2D<T>::getRotatedDeg( float angle ) const {
mbedalvaro 31:5f039cbddee8 307 float a = (float)(angle*DEG_TO_RAD);
mbedalvaro 31:5f039cbddee8 308 return vector2D<T>( x*cos(a) - y*sin(a),
mbedalvaro 31:5f039cbddee8 309 x*sin(a) + y*cos(a) );
mbedalvaro 31:5f039cbddee8 310 }
mbedalvaro 31:5f039cbddee8 311
mbedalvaro 31:5f039cbddee8 312 template <class T>
mbedalvaro 31:5f039cbddee8 313 inline vector2D<T> vector2D<T>::getRotatedRad( float angle ) const {
mbedalvaro 31:5f039cbddee8 314 float a = angle;
mbedalvaro 31:5f039cbddee8 315 return vector2D<T>( x*cos(a) - y*sin(a),
mbedalvaro 31:5f039cbddee8 316 x*sin(a) + y*cos(a) );
mbedalvaro 31:5f039cbddee8 317 }
mbedalvaro 31:5f039cbddee8 318
mbedalvaro 31:5f039cbddee8 319 template <class T>
mbedalvaro 31:5f039cbddee8 320 inline vector2D<T>& vector2D<T>::rotateDeg( float angle ) {
mbedalvaro 31:5f039cbddee8 321 float a = (float)(angle * DEG_TO_RAD);
mbedalvaro 31:5f039cbddee8 322 float xrot = x*cos(a) - y*sin(a);
mbedalvaro 31:5f039cbddee8 323 y = x*sin(a) + y*cos(a);
mbedalvaro 31:5f039cbddee8 324 x = xrot;
mbedalvaro 31:5f039cbddee8 325 return *this;
mbedalvaro 31:5f039cbddee8 326 }
mbedalvaro 31:5f039cbddee8 327
mbedalvaro 31:5f039cbddee8 328 template <class T>
mbedalvaro 31:5f039cbddee8 329 inline vector2D<T>& vector2D<T>::rotateRad( float angle ) {
mbedalvaro 31:5f039cbddee8 330 float a = angle;
mbedalvaro 31:5f039cbddee8 331 float xrot = x*cos(a) - y*sin(a);
mbedalvaro 31:5f039cbddee8 332 y = x*sin(a) + y*cos(a);
mbedalvaro 31:5f039cbddee8 333 x = xrot;
mbedalvaro 31:5f039cbddee8 334 return *this;
mbedalvaro 31:5f039cbddee8 335 }
mbedalvaro 31:5f039cbddee8 336
mbedalvaro 31:5f039cbddee8 337 template <class T>
mbedalvaro 31:5f039cbddee8 338 inline float vector2D<T>::distance( const vector2D<T>& pnt) const {
mbedalvaro 31:5f039cbddee8 339 float vx = x-pnt.x;
mbedalvaro 31:5f039cbddee8 340 float vy = y-pnt.y;
mbedalvaro 31:5f039cbddee8 341 return (float)sqrt(vx*vx + vy*vy);
mbedalvaro 31:5f039cbddee8 342 }
mbedalvaro 31:5f039cbddee8 343
mbedalvaro 31:5f039cbddee8 344 template <class T>
mbedalvaro 31:5f039cbddee8 345 inline float vector2D<T>::squareDistance( const vector2D<T>& pnt ) const {
mbedalvaro 31:5f039cbddee8 346 float vx = x-pnt.x;
mbedalvaro 31:5f039cbddee8 347 float vy = y-pnt.y;
mbedalvaro 31:5f039cbddee8 348 return vx*vx + vy*vy;
mbedalvaro 31:5f039cbddee8 349 }
mbedalvaro 31:5f039cbddee8 350
mbedalvaro 31:5f039cbddee8 351 // Normalization:
mbedalvaro 31:5f039cbddee8 352 template <class T>
mbedalvaro 31:5f039cbddee8 353 inline vector2D<T> vector2D<T>::getNormalized() const {
mbedalvaro 31:5f039cbddee8 354 float length = (float)sqrt(x*x + y*y);
mbedalvaro 31:5f039cbddee8 355 if( length > 0 ) {
mbedalvaro 31:5f039cbddee8 356 return vector2D<T>( x/length, y/length );
mbedalvaro 31:5f039cbddee8 357 } else {
mbedalvaro 31:5f039cbddee8 358 return vector2D<T>();
mbedalvaro 31:5f039cbddee8 359 }
mbedalvaro 31:5f039cbddee8 360 }
mbedalvaro 31:5f039cbddee8 361
mbedalvaro 31:5f039cbddee8 362 template <class T>
mbedalvaro 31:5f039cbddee8 363 inline vector2D<T>& vector2D<T>::normalize() {
mbedalvaro 31:5f039cbddee8 364 float length = (float)sqrt(x*x + y*y);
mbedalvaro 31:5f039cbddee8 365 if( length > 0 ) {
mbedalvaro 31:5f039cbddee8 366 x /= length;
mbedalvaro 31:5f039cbddee8 367 y /= length;
mbedalvaro 31:5f039cbddee8 368 }
mbedalvaro 31:5f039cbddee8 369 return *this;
mbedalvaro 31:5f039cbddee8 370 }
mbedalvaro 31:5f039cbddee8 371
mbedalvaro 31:5f039cbddee8 372 template <class T>
mbedalvaro 31:5f039cbddee8 373 inline vector2D<T> vector2D<T>::getPerpendicularNormed(int orientation) const {
mbedalvaro 31:5f039cbddee8 374 float length = (float)sqrt( x*x + y*y );
mbedalvaro 31:5f039cbddee8 375 if( length > 0 )
mbedalvaro 31:5f039cbddee8 376 return vector2D<T>( -orientation*(y/length), orientation*x/length );
mbedalvaro 31:5f039cbddee8 377 else
mbedalvaro 31:5f039cbddee8 378 return vector2D<T>(0.0, 0.0); // something very small (will be used to compute a force)
mbedalvaro 31:5f039cbddee8 379 }
mbedalvaro 31:5f039cbddee8 380
mbedalvaro 31:5f039cbddee8 381 template <class T>
mbedalvaro 31:5f039cbddee8 382 inline vector2D<T>& vector2D<T>::perpendicular(int orientation) {
mbedalvaro 31:5f039cbddee8 383 float length = (float)sqrt( x*x + y*y );
mbedalvaro 31:5f039cbddee8 384 if( length > 0 ) {
mbedalvaro 31:5f039cbddee8 385 float _x = x;
mbedalvaro 31:5f039cbddee8 386 x = -(y/length)*orientation;
mbedalvaro 31:5f039cbddee8 387 y = _x/length*orientation;
mbedalvaro 31:5f039cbddee8 388 }
mbedalvaro 31:5f039cbddee8 389 return *this;
mbedalvaro 31:5f039cbddee8 390 }
mbedalvaro 31:5f039cbddee8 391
mbedalvaro 31:5f039cbddee8 392 // Length (norm of vector2D<T>):
mbedalvaro 31:5f039cbddee8 393 template <class T>
mbedalvaro 31:5f039cbddee8 394 inline float vector2D<T>::length() const {
mbedalvaro 31:5f039cbddee8 395 return (float)sqrt( x*x + y*y );
mbedalvaro 31:5f039cbddee8 396 }
mbedalvaro 31:5f039cbddee8 397
mbedalvaro 31:5f039cbddee8 398 template <class T>
mbedalvaro 31:5f039cbddee8 399 inline float vector2D<T>::squareLength() const {
mbedalvaro 31:5f039cbddee8 400 return (float)(x*x + y*y);
mbedalvaro 31:5f039cbddee8 401 }
mbedalvaro 31:5f039cbddee8 402
mbedalvaro 31:5f039cbddee8 403 // Angle between two vector2Ds:
mbedalvaro 31:5f039cbddee8 404 template <class T>
mbedalvaro 31:5f039cbddee8 405 inline float vector2D<T>::angleDeg( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 406 return (float)(atan2( x*vec.y-y*vec.x, x*vec.x + y*vec.y )*RAD_TO_DEG);
mbedalvaro 31:5f039cbddee8 407 }
mbedalvaro 31:5f039cbddee8 408
mbedalvaro 31:5f039cbddee8 409 template <class T>
mbedalvaro 31:5f039cbddee8 410 inline float vector2D<T>::angleRad( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 411 return atan2( x*vec.y-y*vec.x, x*vec.x + y*vec.y );
mbedalvaro 31:5f039cbddee8 412 }
mbedalvaro 31:5f039cbddee8 413
mbedalvaro 31:5f039cbddee8 414 template <class T>
mbedalvaro 31:5f039cbddee8 415 inline float vector2D<T>::angleDegHoriz( ) const {
mbedalvaro 31:5f039cbddee8 416 return (float)(atan2( y, x )*RAD_TO_DEG+180); // by adding 180, we get values between 0 and 360 (CCW, with 0 on the left quadrant)
mbedalvaro 31:5f039cbddee8 417 }
mbedalvaro 31:5f039cbddee8 418
mbedalvaro 31:5f039cbddee8 419 //Dot Product:
mbedalvaro 31:5f039cbddee8 420 template <class T>
mbedalvaro 31:5f039cbddee8 421 inline float vector2D<T>::dot( const vector2D<T>& vec ) const {
mbedalvaro 31:5f039cbddee8 422 return x*vec.x + y*vec.y;
mbedalvaro 31:5f039cbddee8 423 }
mbedalvaro 31:5f039cbddee8 424
mbedalvaro 31:5f039cbddee8 425
mbedalvaro 31:5f039cbddee8 426 //handy typedefs:
mbedalvaro 31:5f039cbddee8 427 typedef vector2D<short> vector2Dd;
mbedalvaro 31:5f039cbddee8 428 typedef vector2D<float> vector2Df;
mbedalvaro 31:5f039cbddee8 429
mbedalvaro 31:5f039cbddee8 430
mbedalvaro 31:5f039cbddee8 431 #endif