Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Committer:
mbedalvaro
Date:
Sat Mar 31 12:50:32 2012 +0000
Revision:
2:34157ebbf56b
Parent:
0:345b3bc7a0ea
Child:
12:0de9cd2bced5
- Debugged some subtle problem that made the motion of the rigidLoops strange (when damping factor was too small, the blobs would start moving faster and faster sometimes, and oscillate - the reason being the VERLET integration and the fact that the up...

Who changed what in which revision?

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