00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef __POINT_2D_H
00034 #define __POINT_2D_H
00035
00036 #include "OFELI_Config.h"
00037 #include "Point.h"
00038
00039 namespace OFELI {
00040
00057 template<class T_>
00058 struct Point2D {
00059
00061 Point2D() { x = y = T_(0); }
00062
00065 Point2D(T_ a, T_ b=T_(0)) { x = a; y = b; }
00066
00068 Point2D(T_ *a) { x = a[0]; y = a[1]; }
00069
00071 Point2D(const Point2D<T_> &pt) { x = pt.x; y = pt.y; }
00072
00074 Point2D(const Point<T_> &pt) { x = pt.x; y = pt.y; }
00075
00078 T_ &operator()(size_t i)
00079 {
00080 if (i==2)
00081 return y;
00082 else
00083 return x;
00084 }
00085
00088 const T_ &operator()(size_t i) const
00089 {
00090 if (i==2)
00091 return y;
00092 else
00093 return x;
00094 }
00095
00098 T_ &operator[](size_t i)
00099 {
00100 if (i==1)
00101 return y;
00102 else
00103 return x;
00104 }
00105
00108 const T_ &operator[](size_t i) const
00109 {
00110 if (i==1)
00111 return y;
00112 else
00113 return x;
00114 }
00115
00118 Point2D<T_> & operator=(const Point2D<T_> &p) { x = p.x; y = p.y; return *this; }
00119
00122 Point2D<T_> & operator+=(const Point2D<T_> &p) { x+=p.x; y+=p.y; return *this; }
00123
00126 Point2D<T_> & operator-=(const Point2D<T_> &p) { x-=p.x; y-=p.y; return *this; }
00127
00130 Point2D<T_> & operator=(const T_ &a) { x = y = a; return *this; }
00131
00134 Point2D<T_> & operator+=(const T_ &a) { x+=a; y+=a; return *this; }
00135
00138 Point2D<T_> & operator-=(const T_ &a) { x-=a; y-=a; return *this; }
00139
00142 Point2D<T_> & operator*=(const T_ &a) { x*=a; y*=a; return *this; }
00143
00146 Point2D<T_> & operator/=(const T_ &a) { x/=a; y/=a; return *this; }
00147
00151 bool operator==(const Point2D<T_> &p)
00152 {
00153 if (p.x!=x && p.y!=y)
00154 return false;
00155 return true;
00156 }
00157
00161 bool operator!=(const Point2D<T_> &p)
00162 {
00163 if (p.x!=x && p.y!=y)
00164 return true;
00165 return false;
00166 }
00167
00169 double CrossProduct(const Point2D<double> &lp, const Point2D<double> &rp)
00170 {
00171 return (lp.x*rp.y - lp.y*rp.x);
00172 }
00173
00175 double NNorm() const { return (x*x + y*y); }
00176
00178 double Norm() const { return sqrt(NNorm()); }
00179
00181 Point2D<double> Director(const Point2D<double> &p) const
00182 {
00183 double n = p.Norm();
00184 return Point2D<double>(p.x/n,p.y/n);
00185 }
00186
00188 bool isCloseTo(const Point2D<double> &a, double toler=OFELI_TOLERANCE) const
00189 {
00190 return (*this,a,toler);
00191 }
00192
00194 T_ x;
00195
00197 T_ y;
00198
00199 };
00200
00201
00202
00203
00204
00205
00206
00207
00212 template <class T_>
00213 bool operator== (const Point2D<T_> &a, const Point2D<T_> &b) {
00214 return (a.x==b.x && a.y==b.y);
00215 }
00216
00221 template <class T_>
00222 Point2D<T_> operator+ (const Point2D<T_> &a, const Point2D<T_> &b) { return Point2D<T_>(a.x+b.x,a.y+b.y,a.z+b.z); }
00223
00228 template <class T_>
00229 Point2D<T_> operator+ (const Point2D<T_> &a, const T_ &x) { return Point2D<T_>(a.x+x,a.y+x,a.z+x); }
00230
00231
00236 template <class T_>
00237 Point2D<T_> operator- (const Point2D<T_> &a) { return Point2D<T_>(-a.x,-a.y); }
00238
00239
00244 template <class T_>
00245 Point2D<T_> operator- (const Point2D<T_> &a, const Point2D<T_> &b) { return Point2D<T_>(a.x-b.x,a.y-b.y); }
00246
00247
00252 template <class T_>
00253 Point2D<T_> operator- (const Point2D<T_> &a, const T_ &x) { return Point2D<T_>(a.x-x,a.y-x); }
00254
00255
00260 template <class T_>
00261 Point2D<T_> operator* (const T_ &a, const Point2D<T_> &b) { return Point2D<T_>(a*b.x,a*b.y); }
00262
00263
00268 template <class T_>
00269 Point2D<T_> operator* (const int &a, const Point2D<T_> &b) { return Point2D<T_>(a*b.x,a*b.y); }
00270
00271
00276 template <class T_>
00277 Point2D<T_> operator* (const Point2D<T_> &b, const T_ &a) { return Point2D<T_>(a*b.x,a*b.y);}
00278
00279
00284 template <class T_>
00285 Point2D<T_> operator* (const Point2D<T_> &b, const int &a) { return Point2D<T_>(a*b.x,a*b.y);}
00286
00291 template <class T_>
00292 T_ operator* (const Point2D<T_> &b, const Point2D<T_> &a) { return (a.x*b.x+a.y*b.y); }
00293
00298 template <class T_>
00299 Point2D<T_> operator/ (const Point2D<T_> &b, const T_ &a) { return Point2D<T_>(b.x/a,b.y/a);}
00300
00305 inline bool areClose(const Point2D<double> &a, const Point2D<double> &b, double toler=OFELI_TOLERANCE)
00306 {
00307 if (((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)) < toler*toler)
00308 return true;
00309 else
00310 return false;
00311 }
00312
00316 inline double SqrDistance(const Point2D<double> &a, const Point2D<double> &b)
00317 {
00318 return ((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
00319 }
00320
00324 inline double Distance(const Point2D<double> &a, const Point2D<double> &b)
00325 {
00326 return sqrt(SqrDistance(a,b));
00327 }
00328
00333 template <class T_>
00334 std::ostream & operator<<(std::ostream &s, const Point2D<T_> &a)
00335 {
00336 s.setf(ios::scientific);
00337 s << "( " << std::setprecision(8) << setw(12) << a.x << " , ";
00338 s << setw(12) << a.y << " , " << " )";
00339 return s;
00340 }
00341
00342 }
00343
00344 #endif