00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_VECTOR_2D_H
00027 #define NL_VECTOR_2D_H
00028
00029 #include "nel/misc/types_nl.h"
00030
00031 #include <math.h>
00032 #include <string>
00033
00034 #include "nel/misc/stream.h"
00035 #include "nel/misc/vector_2f.h"
00036
00037 namespace NLMISC
00038 {
00039
00040
00041
00048 class CVector2d
00049 {
00050 public:
00051
00052 public:
00053 double x,y;
00054
00055 public:
00057
00058
00059
00061
00063
00065
00066
00068
00070
00071 CVector2d &operator+=(const CVector2d &v) {x+=v.x; y+=v.y; return *this;}
00072 CVector2d &operator-=(const CVector2d &v) {x-=v.x; y-=v.y; return *this;}
00073 CVector2d &operator*=(double f) {x*=f; y*=f; return *this;}
00074 CVector2d &operator/=(double f) {x/=f; y/=f; return *this;}
00075 CVector2d operator+(const CVector2d &v) const {return CVector2d(x+v.x, y+v.y);}
00076 CVector2d operator-(const CVector2d &v) const {return CVector2d(x-v.x, y-v.y);}
00077 CVector2d operator*(double f) const {return CVector2d(x*f, y*f);}
00078 CVector2d operator/(double f) const {return CVector2d(x/f, y/f);}
00079 CVector2d operator-() const {return CVector2d(-x, -y);}
00081
00083
00084
00085 double operator*(const CVector2d &v) const {return x*v.x + y*v.y;}
00087 double norm() const {return (double)sqrt(sqrnorm());}
00089 double sqrnorm() const {return x*x + y*y;}
00091 void normalize()
00092 {
00093 double f= norm();
00094 if(f>0)
00095 *this/=f;
00096 }
00098 CVector2d normed() const
00099 {
00100 CVector2d v= *this;
00101 v.normalize();
00102 return v;
00103 }
00105
00107
00108 void set(double _x, double _y) {x= _x; y=_y;}
00109 bool operator==(const CVector2d &v) const {return x==v.x && y==v.y;}
00110 bool operator!=(const CVector2d &v) const {return !(*this==v);}
00111 bool isNull() const {return x==0.0f && y==0.0f;}
00113 void minof(const CVector2d &a, const CVector2d &b)
00114 {
00115 x= std::min(a.x, b.x);
00116 y= std::min(a.y, b.y);
00117 }
00119 void maxof(const CVector2d &a, const CVector2d &b)
00120 {
00121 x= std::max(a.x, b.x);
00122 y= std::max(a.y, b.y);
00123 }
00125 void serial(NLMISC::IStream &f) {f.serial(x,y);}
00127
00128
00129 friend CVector2d operator*(double f, const CVector2d &v0);
00130
00131 };
00132
00133
00134 inline CVector2d operator*(double f, const CVector2d &v)
00135 {
00136 return v*f;
00137 }
00138
00139
00140 }
00141
00142
00143 #endif // NL_VECTOR_2D_H
00144
00145