Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Fri Sep 21 10:02:35 2012 +0000
Revision:
30:d8af03f01cd4
Parent:
18:d72935b13858
Child:
31:5f039cbddee8
first commit. Not yet functional. Added ghost and pacman game modes, but the behaviour of these "rigid spots" is not implemented yet

Who changed what in which revision?

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