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 x, double y)
00046         : m_x(x), m_y(y) {}
00047 
00048 
00059       Vector2D(double x, double y, double alpha)
00060         : m_x(x), m_y(y) {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 
00088       inline void setValue(double x, double y) {
00089         m_x = x; m_y = y;
00090       }
00091 
00092 
00103       inline void setValue(double x, double y, double alpha) {
00104         m_x = x; m_y = y; normalize(alpha);
00105       }
00106 
00107 
00114       inline double& x() {return m_x;}
00115 
00116 
00123       inline double x() const {return m_x;}
00124 
00125     
00132       inline double& y() {return m_y;}
00133 
00134     
00141       inline double y() const {return m_y;}
00142     
00143 
00153       Vector2D& operator=(const Vector2D& vec) {
00154         setValue(vec.m_x, vec.m_y); return *this; // Self-assignment OK.
00155       }
00156 
00157 
00167       Vector2D& operator*=(double scalar) {
00168         m_x *= scalar; m_y *= scalar; return *this;
00169       }
00170 
00171 
00181       Vector2D& operator/=(double scalar) {
00182         if (scalar == 0 ) {
00183           DLR_THROW(ValueException, "Vector2D::operator/=()",
00184                     "Bad scalar: Divide by Zero\n");
00185         }
00186         m_x /= scalar; m_y /= scalar; return *this;
00187       }
00188 
00189 
00199       Vector2D& operator+=(const Vector2D& vec) {
00200         m_x += vec.m_x; m_y += vec.m_y; return *this;
00201       }
00202 
00203 
00213       Vector2D& operator-=(const Vector2D& vec) {
00214         m_x -= vec.m_x; m_y -= vec.m_y; return *this;
00215       }
00216 
00217 
00224       Vector2D operator-() {
00225         return Vector2D(-m_x, -m_y);
00226       }
00227     
00228     private:
00229       // Private member functions.
00230       inline void normalize(double alpha) {
00231         if(alpha == 1.0) {return;}
00232         if(alpha == 0.0) {
00233           DLR_THROW(ValueException, "Vector2D::normalize()",
00234                     "Bad alpha (0.0).");
00235         }
00236         m_x /= alpha; m_y /= alpha;
00237         return;
00238       }
00239 
00240       // Private data members.
00241       double m_x;
00242       double m_y;
00243     }; // class Vector2D
00244 
00245 
00246     /* ============== Non-member function declarations ============== */
00247   
00258     Vector2D
00259     operator+(const Vector2D& vector0, const Vector2D& vector1);
00260 
00273     Vector2D
00274     operator-(const Vector2D& vector0, const Vector2D& vector1);
00275   
00286     Vector2D
00287     operator*(const Vector2D& vector0, const Vector2D& vector1);
00288 
00300     Vector2D
00301     operator/(const Vector2D& vector0, const Vector2D& vector1);
00302 
00314     Vector2D operator+(const Vector2D& vector0, double scalar0);
00315 
00327     Vector2D operator-(const Vector2D& vector0, double scalar0);
00328 
00340     Vector2D operator*(const Vector2D& vector0, double scalar0);
00341 
00353     Vector2D operator/(const Vector2D& vector0, double scalar0);
00354 
00362     bool operator==(const Vector2D& vector0, const Vector2D& vector1);
00363 
00372     bool operator!=(const Vector2D& vector0, const Vector2D& vector1);
00373 
00374 
00387     Vector2D operator+(double scalar0, const Vector2D& vector0);
00388 
00389 
00402     Vector2D operator*(double scalar0, const Vector2D& vector0);
00403 
00404   
00420     std::ostream& operator<<(std::ostream& stream, const Vector2D& vector0);
00421 
00436     std::istream& operator>>(std::istream& stream, Vector2D& vector0);
00437 
00438   } // namespace numeric
00439 
00440 } // namespace dlr
00441 
00442 
00443 /* ======= Declarations to maintain compatibility with legacy code. ======= */
00444 
00445 namespace dlr {
00446 
00447   using numeric::Vector2D;
00448 
00449 } // namespace dlr
00450 
00451 
00452 /* ============ Definitions of inline & template functions ============ */
00453 
00454 namespace dlr {
00455 
00456   namespace numeric {
00457     
00458     inline Vector2D operator+(double scalar0, const Vector2D& vector0)
00459     {
00460       return vector0 + scalar0;
00461     }
00462   
00463     inline Vector2D operator*(double scalar0, const Vector2D& vector0)
00464     {
00465       return vector0 * scalar0;
00466     }
00467 
00468   } // namespace numeric
00469 
00470 } // namespace dlr
00471 
00472 #endif // #ifndef _DLR_VECTOR2D_H_

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2