Laser Sensing Display for UI interfaces in the real world

Dependencies:   mbed

Fork of skinGames_forktest by Alvaro Cassinelli

Revision:
12:0de9cd2bced5
Parent:
2:34157ebbf56b
Child:
18:d72935b13858
--- a/myVectorClass.h	Thu Apr 12 05:16:48 2012 +0000
+++ b/myVectorClass.h	Thu Apr 12 08:38:44 2012 +0000
@@ -25,16 +25,16 @@
 #define PI 3.14159265889
 #endif
 
-
+template <class T>
 class vector2D {
 
     public:
         
         // Overloaded constructor with parameters:
-        vector2D( float _x=0.0f, float _y=0.0f );
+        vector2D( T _x=0.0f, T _y=0.0f );
         
         // Explicit setting:
-        void set( float _x, float _y );
+        void set( T _x, T _y );
         void set( const vector2D& vec );
         
         // Comparison:
@@ -56,17 +56,20 @@
         vector2D& operator/=( const vector2D& vec );
     
     //operator overloading for float:
-    void       operator=( const float f);  // I cannot declare this if we want also operator chaining?
+    void       operator=( const T f);  // I cannot declare this if we want also operator chaining?
     //vector2D & operator=( const float& val ); // to allow operator chaining
-    vector2D  operator+( const float f ) const;
-    vector2D& operator+=( const float f );
-    vector2D  operator-( const float f ) const;
-    vector2D& operator-=( const float f );
+    vector2D  operator+( const T f ) const;
+    vector2D& operator+=( const T f );
+    vector2D  operator-( const T f ) const;
+    vector2D& operator-=( const T f );
     vector2D  operator-() const;
-    vector2D  operator*( const float f ) const;
-    vector2D& operator*=( const float f );
-    vector2D  operator/( const float f ) const;
-    vector2D& operator/=( const float f );    
+
+    
+    // multiplication by a scalar:
+    vector2D  operator*( const T f ) const;
+    vector2D& operator*=( const T f );
+    vector2D  operator/( const T f ) const;
+    vector2D& operator/=( const T f );    
     
     // Distance (between end points of two vector2Ds):
     float distance( const vector2D& pnt) const;
@@ -109,7 +112,7 @@
     // =================================================================
     
     // Actual variables:
-    float x, y; // or make a class "point"
+    T x, y; // or make a class "point"
     
 };
 
@@ -117,33 +120,34 @@
 // Implementation
 /////////////////
 
-
-inline vector2D::vector2D( float _x, float _y ) {
-    x = _x;
-    y = _y;
+template <class T>
+inline vector2D<T>::vector2D( T _x, T _y ) {
+    x = _x; y = _y;
 }
 
-inline void vector2D::set( float _x, float _y ) {
-    x = _x;
-    y = _y;
+template <class T>
+inline void vector2D<T>::set( T _x, T _y ) {
+    x = _x; y = _y;
 }
 
-inline void vector2D::set( const vector2D& vec ) {
-    x=vec.x;
-    y=vec.y;
+template <class T>
+inline void vector2D<T>::set( const vector2D<T>& vec ) {
+    x=vec.x; y=vec.y;
 }
 
-inline bool vector2D::operator==( const vector2D& vec ) {
+template <class T>
+inline bool vector2D<T>::operator==( const vector2D<T>& vec ) {
     return (x == vec.x) && (y == vec.y);
 }
 
-inline bool vector2D::operator!=( const vector2D& vec ) {
+template <class T>
+inline bool vector2D<T>::operator!=( const vector2D<T>& vec ) {
     return (x != vec.x) || (y != vec.y);
 }
 
-inline bool vector2D::match( const vector2D& vec, float tollerance ) {
-    return (abs(x - vec.x) < tollerance)
-    && (abs(y - vec.y) < tollerance);
+template <class T>
+inline bool vector2D<T>::match( const vector2D<T>& vec, float tolerance ) {
+    return (abs(x - vec.x) < tolerance)&& (abs(y - vec.y) < tolerance);
 }
 
 
@@ -155,48 +159,54 @@
 }
  */
 
-
-inline void vector2D::operator=( const vector2D& vec ){
-    x = vec.x;
-    y = vec.y;
+template <class T>
+inline void vector2D<T>::operator=( const vector2D<T>& vec ){
+    x = vec.x;  y = vec.y;
 }
 
-
-inline vector2D vector2D::operator+( const vector2D& vec ) const {
-    return vector2D( x+vec.x, y+vec.y);
+template <class T>
+inline vector2D<T> vector2D<T>::operator+( const vector2D<T>& vec ) const {
+    return vector2D<T>( x+vec.x, y+vec.y);
 }
 
-inline vector2D& vector2D::operator+=( const vector2D& vec ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::operator+=( const vector2D<T>& vec ) {
     x += vec.x;
     y += vec.y;
     return *this;
 }
 
-inline vector2D vector2D::operator-( const vector2D& vec ) const {  
-    return vector2D(x-vec.x, y-vec.y);
+template <class T>
+inline vector2D<T> vector2D<T>::operator-( const vector2D<T>& vec ) const {  
+    return vector2D<T>(x-vec.x, y-vec.y);
 }
 
-inline vector2D& vector2D::operator-=( const vector2D& vec ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::operator-=( const vector2D<T>& vec ) {
     x -= vec.x;
     y -= vec.y;
     return *this;
 }
 
-inline vector2D vector2D::operator*( const vector2D& vec ) const {
-    return vector2D(x*vec.x, y*vec.y);
+template <class T>
+inline vector2D<T> vector2D<T>::operator*( const vector2D<T>& vec ) const {
+    return vector2D<T>(x*vec.x, y*vec.y);
 }
 
-inline vector2D& vector2D::operator*=( const vector2D& vec ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::operator*=( const vector2D<T>& vec ) {
     x*=vec.x;
     y*=vec.y;
     return *this;
 }
 
-inline vector2D vector2D::operator/( const vector2D& vec ) const {
-    return vector2D( vec.x!=0 ? x/vec.x : x , vec.y!=0 ? y/vec.y : y);
+template <class T>
+inline vector2D<T> vector2D<T>::operator/( const vector2D<T>& vec ) const {
+    return vector2D<T>( vec.x!=0 ? x/vec.x : x , vec.y!=0 ? y/vec.y : y);
 }
 
-inline vector2D& vector2D::operator/=( const vector2D& vec ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::operator/=( const vector2D<T>& vec ) {
     vec.x!=0 ? x/=vec.x : x;
     vec.y!=0 ? y/=vec.y : y;
     return *this;
@@ -204,77 +214,81 @@
 
 //operator overloading for float:
 /*
-inline vector2D & operator=( const float& val ){
+inline vector2D<T> & operator=( const float& val ){
     x = val;
     y = val;
     return *this;
 }
  */
 
-inline void vector2D::operator=( const float f){
-    x = f;
-    y = f;
+template <class T>
+inline void vector2D<T>::operator=( const T f){
+    x = f;  y = f;
 }
 
-
-inline vector2D vector2D::operator+( const float f ) const {
-    return vector2D( x+f, y+f);
+template <class T>
+inline vector2D<T> vector2D<T>::operator+( const T f ) const {
+    return vector2D<T>( x+f, y+f);
 }
 
-inline vector2D& vector2D::operator+=( const float f ) {
-    x += f;
-    y += f;
+template <class T>
+inline vector2D<T>& vector2D<T>::operator+=( const T f ) {
+    x += f; y += f;
     return *this;
 }
 
-inline vector2D vector2D::operator-( const float f ) const {
-    return vector2D( x-f, y-f);
+template <class T>
+inline vector2D<T> vector2D<T>::operator-( const T f ) const {
+    return vector2D<T>( x-f, y-f);
 }
 
-inline vector2D& vector2D::operator-=( const float f ) {
-    x -= f;
-    y -= f;
+template <class T>
+inline vector2D<T>& vector2D<T>::operator-=( const T f ) {
+    x -= f; y -= f;
     return *this;
 }
 
-inline vector2D vector2D::operator-() const {
-    return vector2D(-x, -y);
+template <class T>
+inline vector2D<T> vector2D<T>::operator-() const {
+    return vector2D<T>(-x, -y);
 }
 
-inline vector2D vector2D::operator*( const float f ) const {
-    return vector2D(x*f, y*f);
+template <class T>
+inline vector2D<T> vector2D<T>::operator*( const T f ) const {
+    return vector2D<T>(x*f, y*f);
 }
 
-inline vector2D& vector2D::operator*=( const float f ) {
-    x*=f;
-    y*=f;
+template <class T>
+inline vector2D<T>& vector2D<T>::operator*=( const T f ) {
+    x*=f; y*=f;
     return *this;
 }
 
-inline vector2D vector2D::operator/( const float f ) const {
+template <class T>
+inline vector2D<T> vector2D<T>::operator/( const T f ) const {
     //cout << "here" << endl;
-    if(f == 0) return vector2D(x, y);
-    return vector2D(x/f, y/f);
+    if(f == 0) return vector2D<T>(x, y);
+    return vector2D<T>(x/f, y/f);
 }
 
-inline vector2D& vector2D::operator/=( const float f ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::operator/=( const T f ) {
     if(f == 0) return *this;
-    x/=f;
-    y/=f;
+    x/=f; y/=f;
     return *this;
 }
 
-
-inline vector2D vector2D::getScaled( const float length ) const {
+template <class T>
+inline vector2D<T> vector2D<T>::getScaled( const float length ) const {
     float l = (float)sqrt(x*x + y*y);
     if( l > 0 )
-        return vector2D( (x/l)*length, (y/l)*length );
+        return vector2D<T>( (x/l)*length, (y/l)*length );
     else
-        return vector2D();
+        return vector2D<T>();
 }
 
-
-inline vector2D& vector2D::scale( const float length ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::scale( const float length ) {
     float l = (float)sqrt(x*x + y*y);
     if (l > 0) {
         x = (x/l)*length;
@@ -287,19 +301,22 @@
 //
 //
 
-inline vector2D vector2D::getRotatedDeg( float angle ) const {
+template <class T>
+inline vector2D<T> vector2D<T>::getRotatedDeg( float angle ) const {
     float a = (float)(angle*DEG_TO_RAD);
-    return vector2D( x*cos(a) - y*sin(a),
+    return vector2D<T>( x*cos(a) - y*sin(a),
                     x*sin(a) + y*cos(a) );
 }
 
-inline vector2D vector2D::getRotatedRad( float angle ) const {
+template <class T> 
+inline vector2D<T> vector2D<T>::getRotatedRad( float angle ) const {
     float a = angle;
-    return vector2D( x*cos(a) - y*sin(a),
+    return vector2D<T>( x*cos(a) - y*sin(a),
                     x*sin(a) + y*cos(a) );
 }
 
-inline vector2D& vector2D::rotateDeg( float angle ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::rotateDeg( float angle ) {
     float a = (float)(angle * DEG_TO_RAD);
     float xrot = x*cos(a) - y*sin(a);
     y = x*sin(a) + y*cos(a);
@@ -307,7 +324,8 @@
     return *this;
 }
 
-inline vector2D& vector2D::rotateRad( float angle ) {
+template <class T>
+inline vector2D<T>& vector2D<T>::rotateRad( float angle ) {
     float a = angle;
     float xrot = x*cos(a) - y*sin(a);
     y = x*sin(a) + y*cos(a);
@@ -315,29 +333,33 @@
     return *this;
 }
 
-inline float vector2D::distance( const vector2D& pnt) const {
+template <class T>
+inline float vector2D<T>::distance( const vector2D<T>& pnt) const {
     float vx = x-pnt.x;
     float vy = y-pnt.y;
     return (float)sqrt(vx*vx + vy*vy);
 }
 
-inline float vector2D::squareDistance( const vector2D& pnt ) const {
+template <class T>
+inline float vector2D<T>::squareDistance( const vector2D<T>& pnt ) const {
     float vx = x-pnt.x;
     float vy = y-pnt.y;
     return vx*vx + vy*vy;
 }
 
 // Normalization:
-inline vector2D vector2D::getNormalized() const {
+template <class T>
+inline vector2D<T> vector2D<T>::getNormalized() const {
     float length = (float)sqrt(x*x + y*y);
     if( length > 0 ) {
-        return vector2D( x/length, y/length );
+        return vector2D<T>( x/length, y/length );
     } else {
-        return vector2D();
+        return vector2D<T>();
     }
 }
 
-inline vector2D& vector2D::normalize() {
+template <class T>
+inline vector2D<T>& vector2D<T>::normalize() {
     float length = (float)sqrt(x*x + y*y);
     if( length > 0 ) {
         x /= length;
@@ -346,15 +368,17 @@
     return *this;
 }
 
-inline vector2D vector2D::getPerpendicularNormed(int orientation) const {
+template <class T>
+inline vector2D<T> vector2D<T>::getPerpendicularNormed(int orientation) const {
     float length = (float)sqrt( x*x + y*y );
     if( length > 0 )
-        return vector2D( -orientation*(y/length), orientation*x/length );
+        return vector2D<T>( -orientation*(y/length), orientation*x/length );
     else
-        return vector2D(0.0, 0.0); // something very small (will be used to compute a force)
+        return vector2D<T>(0.0, 0.0); // something very small (will be used to compute a force)
 }
 
-inline vector2D& vector2D::perpendicular(int orientation) {
+template <class T>
+inline vector2D<T>& vector2D<T>::perpendicular(int orientation) {
     float length = (float)sqrt( x*x + y*y );
     if( length > 0 ) {
         float _x = x;
@@ -364,27 +388,36 @@
     return *this;
 }
 
-// Length (norm of vector2D):
-inline float vector2D::length() const {
+// Length (norm of vector2D<T>):
+template <class T>
+inline float vector2D<T>::length() const {
     return (float)sqrt( x*x + y*y );
 }
 
-inline float vector2D::squareLength() const {
+template <class T>
+inline float vector2D<T>::squareLength() const {
     return (float)(x*x + y*y);
 }
 
 // Angle between two vector2Ds:
-inline float vector2D::angleDeg( const vector2D& vec ) const {
+template <class T>
+inline float vector2D<T>::angleDeg( const vector2D<T>& vec ) const {
     return (float)(atan2( x*vec.y-y*vec.x, x*vec.x + y*vec.y )*RAD_TO_DEG);
 }
 
-inline float vector2D::angleRad( const vector2D& vec ) const {
+template <class T>
+inline float vector2D<T>::angleRad( const vector2D<T>& vec ) const {
     return atan2( x*vec.y-y*vec.x, x*vec.x + y*vec.y );
 }
 
-inline float vector2D::angleDegHoriz( ) const {
+template <class T>
+inline float vector2D<T>::angleDegHoriz( ) const {
     return (float)(atan2( y, x )*RAD_TO_DEG);
 }
 
+//handy typedefs:
+typedef vector2D<short> vector2Dd;
+typedef vector2D<float> vector2Df;
+
 
 #endif