00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #ifndef HEADER_VECTOR2D_HXX
00021 #define HEADER_VECTOR2D_HXX
00022 
00023 #include <math.h>
00024 #include <iostream>
00025 
00027 class Vector2d
00028 {
00029 public:
00030   float x;
00031   float y;
00032   
00033   Vector2d ()
00034     : x(0), y(0)
00035   {}
00036 
00037   Vector2d (float x_, float y_) 
00038     : x (x_), y (y_)
00039   {}
00040 
00041   inline 
00042   void operator+= (const Vector2d& vec) {
00043     x += vec.x;
00044     y += vec.y;
00045   }
00046 
00047   inline 
00048   void operator-= (const Vector2d& vec) {
00049     x -= vec.x;
00050     y -= vec.y;
00051   }
00052 
00053   inline 
00054   void operator*= (float f) {
00055     x *= f;
00056     y *= f;
00057   }
00058 
00059   inline 
00060   Vector2d operator+ (const Vector2d& vec) const  {
00061     return Vector2d(x + vec.x, y + vec.y);
00062   }
00063 
00064   inline
00065   float dot(const Vector2d& vec) const {
00066     return (x * vec.x) + (y * vec.y);
00067   }
00068 
00069   inline 
00070   Vector2d operator- () const  {
00071     return Vector2d(-x, -y);
00072   }
00073 
00074   inline 
00075   Vector2d operator- (const Vector2d& vec) const  {
00076     return Vector2d(x - vec.x, y - vec.y);
00077   }
00078 
00079   inline 
00080   Vector2d operator* (float f) const  {
00081     return Vector2d(x * f, y * f);
00082   }
00083 
00084   inline 
00085   float norm() const {
00086     return sqrt (x*x + y*y);
00087   }
00088 
00089 
00090   inline 
00091   void normalize() {
00092     float f = norm();
00093     if (f!=0)
00094       {
00095         x /= f;
00096         y /= f;
00097       }
00098   }
00099 
00100 };
00101 
00102 inline 
00103 std::ostream& operator << (std::ostream& os, const Vector2d& v)
00104 {
00105   return os << "[" << v.x << ", " << v.y << "]";
00106 }
00107 
00108 #endif
00109 
00110