Open PaperOpt
OpenPaperOpt/V2.h
Go to the documentation of this file.
00001 #ifndef V2_H
00002 #define V2_H
00003 
00004 
00005 #include <math.h>
00006 
00007 #ifndef PI
00008 #define PI 3.1415926535897932384626433832795
00009 #endif
00010 
00011 template <class T>
00012 class V2{
00013 private:
00014         T x, y;
00015 
00016 public:
00017         V2() : x(0), y(0) {}
00018         V2(const T _x, const T _y) : x(_x), y(_y) {}
00019         V2(const V2 <T>& v) : x(v.x), y(v.y) {}
00020 
00021         T X()      const { return x; }
00022         T Y()      const { return y; }
00023         T P(int i) const {
00024                 switch (i) {
00025                         case 0: return x;
00026                         case 1: return y;
00027                         default: return 0;
00028                 }
00029         }
00030 
00031         void X(T _x) { x = _x; }
00032         void Y(T _y) { y = _y; }
00033 
00034         void NormalizeVector(){
00035                 double temp = sqrt(x*x + y*y);
00036                 if (temp!=0){
00037                         x /= temp;
00038                         y /= temp;
00039                 }
00040         }
00041 
00042         T LengthVector() { return sqrt(x*x + y*y); }
00043         T LengthVector2() { return x*x + y*y; }
00044 
00045         V2 <T> &    Rotate90()     {T t=x;x=-y;y=t;return *this;}       
00046 
00047         void operator+=(const V2 <T> & v) { x += v.x; y += v.y; }
00048         void operator-=(const V2 <T> & v) { x -= v.x; y -= v.y; }
00049         void operator*=(const V2 <T> & v) { x *= v.x; y *= v.y; }
00050         void operator/=(const V2 <T> & v) { x/=v.x; y/=v.y; }
00051 
00052         void operator*=(T f)     {x*=f;y*=f;}   
00053     void operator/=(T f)     {operator*=((T)(1.0/f));}  
00054     V2 <T> operator-(void) const   {return V2 <T> (-x,-y);}     
00055 
00056         void VectDivConst(const double& v) { x /= v; y /= v; }
00057         void VectMulConst(const double& v) { x *= v; y *= v; }
00058 
00059 };
00060 template <class T>
00061 inline V2 <T>  operator+(const V2 <T> & a,const V2 <T> & b) {
00062         return V2 <T> (a.X()+b.X(),a.Y()+b.Y());
00063 }
00064 template <class T>
00065 inline V2 <T> operator-(const V2 <T> & a,const V2 <T> & b){
00066         return V2 <T> (a.X()-b.X(),a.Y()-b.Y());
00067 }
00068 template <class T>
00069 inline T operator*(const V2 <T> & a,const V2 <T> & b) {
00070         return a.X()*b.X()+a.Y()*b.Y();
00071 }
00072 template <class T>
00073 inline V2 <T> operator*(const V2 <T> & a,T f){
00074         return V2 <T> (a.X()*f,a.Y()*f);
00075 }
00076 template <class T>
00077 inline V2 <T> operator/(const V2 <T> & a,T f){
00078         return a*(T)(1.0/f);
00079 }
00080 
00081 template <class T>
00082 inline V2 <T> elemMul(const V2 <T> & a,const V2 <T>& b){
00083         return V2 <T> (a.X()*b.X(),a.Y()*b.Y());
00084 }
00085 
00092 template <class T>
00093 inline int CrossLineToLineseg(const V2 <T>& a1,const V2 <T>& a2,const V2<T>& b1,const V2 <T>& b2) {
00094 
00095 
00096   T nX=a1.Y()-a2.Y();//Get a normal of first linesegment
00097   T nY=a2.X()-a1.X();
00098   T d=nX*a1.X()+nY*a1.Y(); //Calc d in line equation n*p=d
00099   T left =nX*b1.X()+nY*b1.Y()-d; //Calc (n*b1-d)*(n*b2-d). If result is negative 
00100   T right=nX*b2.X()+nY*b2.Y()-d;
00101   return (left*right<0)? 1:0;
00102 }
00103 
00104 #define AREA(V1,V2,V3,V4) (fabs(0.5*((V1).X()*(V2).Y()+(V2).X()*(V3).Y()+(V3).X()*(V4).Y()+(V4).X()*(V1).Y()-(V2).X()*(V1).Y()-(V3).X()*(V2).Y()-(V4).X()*(V3).Y()-(V1).X()*(V4).Y())))
00105 
00106 //First vector is 0,0
00107 #define AREA0(V2,V3,V4) (fabs(0.5*((V2).X()*(V3).Y()+(V3).X()*(V4).Y()-(V3).X()*(V2).Y()-(V4).X()*(V3).Y())))
00108 
00109 //----------------------------------------------------------------------------------------
00110 
00111 #endif
00112 
00113