vector2D.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLR_VECTOR2D_H_
00016 #define _DLR_VECTOR2D_H_
00017 
00018 #include <iostream>
00019 #include <dlrCommon/exception.h>
00020 
00021 namespace dlr {
00022 
00023   namespace numeric {
00024     
00028     class Vector2D {
00029     public:
00030 
00034       Vector2D()
00035         : m_x(0), m_y(0) {}
00036     
00037 
00045       Vector2D(double xCoord, double yCoord)
00046         : m_x(xCoord), m_y(yCoord) {}
00047 
00048 
00059       Vector2D(double xCoord, double yCoord, double alpha)
00060         : m_x(xCoord), m_y(yCoord) {this->normalize(alpha);}
00061 
00062 
00069       Vector2D(const Vector2D& vec)
00070         : m_x(vec.m_x), m_y(vec.m_y) {}
00071 
00072 
00076       ~Vector2D() {}
00077 
00078 
00084       inline Vector2D& clear() {
00085         m_x = 0.0; m_y = 0.0;
00086         return *this;
00087       }
00088 
00089       
00099       inline void setValue(double xCoord, double yCoord) {
00100         m_x = xCoord; m_y = yCoord;
00101       }
00102 
00103 
00114       inline void setValue(double xCoord, double yCoord, double alpha) {
00115         m_x = xCoord; m_y = yCoord; normalize(alpha);
00116       }
00117 
00118 
00125       inline double& x() {return m_x;}
00126 
00127 
00134       inline double x() const {return m_x;}
00135 
00136     
00143       inline double& y() {return m_y;}
00144 
00145     
00152       inline double y() const {return m_y;}
00153     
00154 
00164       Vector2D& operator=(const Vector2D& vec) {
00165         setValue(vec.m_x, vec.m_y); return *this; // Self-assignment OK.
00166       }
00167 
00168 
00178       Vector2D& operator*=(double scalar) {
00179         m_x *= scalar; m_y *= scalar; return *this;
00180       }
00181 
00182 
00192       Vector2D& operator/=(double scalar) {
00193         if (scalar == 0.0) {
00194           DLR_THROW(ValueException, "Vector2D::operator/=()",
00195                     "Bad scalar: Divide by Zero\n");
00196         }
00197         m_x /= scalar; m_y /= scalar; return *this;
00198       }
00199 
00200 
00210       Vector2D& operator+=(const Vector2D& vec) {
00211         m_x += vec.m_x; m_y += vec.m_y; return *this;
00212       }
00213 
00214 
00224       Vector2D& operator-=(const Vector2D& vec) {
00225         m_x -= vec.m_x; m_y -= vec.m_y; return *this;
00226       }
00227 
00228 
00235       Vector2D operator-() {
00236         return Vector2D(-m_x, -m_y);
00237       }
00238     
00239     private:
00240       // Private member functions.
00241       inline void normalize(double alpha) {
00242         if(alpha == 1.0) {return;}
00243         if(alpha == 0.0) {
00244           DLR_THROW(ValueException, "Vector2D::normalize()",
00245                     "Bad alpha (0.0).");
00246         }
00247         m_x /= alpha; m_y /= alpha;
00248         return;
00249       }
00250 
00251       // Private data members.
00252       double m_x;
00253       double m_y;
00254     }; // class Vector2D
00255 
00256 
00257     /* ============== Non-member function declarations ============== */
00258   
00269     Vector2D
00270     operator+(const Vector2D& vector0, const Vector2D& vector1);
00271 
00284     Vector2D
00285     operator-(const Vector2D& vector0, const Vector2D& vector1);
00286   
00297     Vector2D
00298     operator*(const Vector2D& vector0, const Vector2D& vector1);
00299 
00311     Vector2D
00312     operator/(const Vector2D& vector0, const Vector2D& vector1);
00313 
00325     Vector2D operator+(const Vector2D& vector0, double scalar0);
00326 
00338     Vector2D operator-(const Vector2D& vector0, double scalar0);
00339 
00351     Vector2D operator*(const Vector2D& vector0, double scalar0);
00352 
00364     Vector2D operator/(const Vector2D& vector0, double scalar0);
00365 
00373     bool operator==(const Vector2D& vector0, const Vector2D& vector1);
00374 
00383     bool operator!=(const Vector2D& vector0, const Vector2D& vector1);
00384 
00385 
00398     Vector2D operator+(double scalar0, const Vector2D& vector0);
00399 
00400 
00413     Vector2D operator*(double scalar0, const Vector2D& vector0);
00414 
00415   
00431     std::ostream& operator<<(std::ostream& stream, const Vector2D& vector0);
00432 
00447     std::istream& operator>>(std::istream& stream, Vector2D& vector0);
00448 
00449   } // namespace numeric
00450 
00451 } // namespace dlr
00452 
00453 
00454 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00455 
00456 namespace dlr {
00457 
00458   using numeric::Vector2D;
00459 
00460 } // namespace dlr
00461 
00462 
00463 /* ============ Definitions of inline & template functions ============ */
00464 
00465 namespace dlr {
00466 
00467   namespace numeric {
00468     
00469     inline Vector2D operator+(double scalar0, const Vector2D& vector0)
00470     {
00471       return vector0 + scalar0;
00472     }
00473   
00474     inline Vector2D operator*(double scalar0, const Vector2D& vector0)
00475     {
00476       return vector0 * scalar0;
00477     }
00478 
00479   } // namespace numeric
00480 
00481 } // namespace dlr
00482 
00483 #endif // #ifndef _DLR_VECTOR2D_H_

Generated on Tue Jan 6 17:52:07 2009 for dlrUtilities Utility Library by  doxygen 1.5.6