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