00001 /*************************************************************** 00002 00003 Intelligent Ground Vehicle Competition 2010 00004 Pennsylvania State University - Robotics Club 00005 Learn more at www.psurobotics.org 00006 Protected by the GNU General Public License 00007 00008 This source file is developed and maintained by: 00009 + Jeremy Bridon jbridon@psu.edu 00010 00011 File: Vector2.h 00012 Desc: A two-dimensional vector with custom overloaded operators 00013 00014 ***************************************************************/ 00015 00016 // Inclusion guard 00017 #ifndef __VECTOR2_H_ 00018 #define __VECTOR2_H_ 00019 00020 // Includes 00021 #include "math.h" 00022 00024 template <typename Type> class Vector2 00025 { 00026 public: 00027 00029 Vector2() 00030 { 00031 x = (Type)0; 00032 y = (Type)0; 00033 } 00034 00036 Vector2(Type x, Type y) 00037 { 00038 this->x = x; 00039 this->y = y; 00040 } 00041 00043 Vector2(const Vector2 &obj) 00044 { 00045 x = obj.x; 00046 y = obj.y; 00047 } 00048 00050 ~Vector2() 00051 { 00052 // Nothing to do... 00053 } 00054 00056 Vector2 operator= (const Vector2 &obj) 00057 { 00058 x = obj.x; 00059 y = obj.y; 00060 return *this; 00061 } 00062 00064 Vector2 operator+ (const Vector2 &obj) 00065 { 00066 return Vector2<Type>(x + obj.x, y + obj.y); 00067 } 00068 00070 Vector2 operator- (const Vector2 &obj) 00071 { 00072 return Vector2<Type>(x - obj.x, y - obj.y); 00073 } 00074 00076 Vector2 operator* (const Vector2 &obj) 00077 { 00078 return Vector2<Type>(x * obj.x, y * obj.y); 00079 } 00080 00082 Vector2 operator/ (const Vector2 &obj) 00083 { 00084 return Vector2<Type>(x / obj.x, y / obj.y); 00085 } 00086 00088 Vector2& operator+= (const Vector2 &obj) 00089 { 00090 *this = *this + obj; 00091 return *this; 00092 } 00093 00095 Vector2& operator-= (const Vector2 &obj) 00096 { 00097 *this = *this - obj; 00098 return *this; 00099 } 00100 00102 Vector2& operator*= (const Vector2 &obj) 00103 { 00104 *this = *this * obj; 00105 return *this; 00106 } 00107 00109 Vector2& operator/= (const Vector2 &obj) 00110 { 00111 *this = *this / obj; 00112 return *this; 00113 } 00114 00116 bool operator== (const Vector2 &obj) 00117 { 00118 if(obj.x == x && obj.y == y) 00119 return true; 00120 else 00121 return false; 00122 } 00123 00125 bool operator!= (const Vector2 &obj) 00126 { 00127 if(obj.x != x || obj.y != y) 00128 return true; 00129 else 00130 return false; 00131 } 00132 00134 double GetLength() 00135 { 00136 double X = (double)x; 00137 double Y = (double)y; 00138 return sqrt(X*X + Y*Y); 00139 } 00140 00142 Type x, y; 00143 00144 }; 00145 00146 // Inclusion guard 00147 #endif
1.5.5