00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef FILE_CURVE_HXX
00021
00022 #include <stdio.h>
00023 #include <math.h>
00024
00025 #include <string>
00026 #include <sstream>
00027 #include <fstream>
00028
00029 using std::string;
00030 using std::istringstream;
00031 using std::ifstream;
00032 using std::getline;
00033
00034
00035 namespace Multivac
00036 {
00037
00038
00040
00045 template <class T>
00046 T DistanceBetween(Vector<T>& A, Vector<T>& B)
00047 {
00048
00049 T tempx = A(0) - B(0);
00050 T tempy = A(1) - B(1);
00051
00052 return sqrt( tempx*tempx + tempy*tempy );
00053
00054 }
00055
00056
00058
00067 template <class T>
00068 bool Intersect(Vector<T>& A, Vector<T>& B,
00069 Vector<T>& C, Vector<T>& D)
00070 {
00071
00072 T slopeAB, slopeCD;
00073
00074 slopeAB = B(0) - A(0);
00075
00076 if (B(0) == D(0) && B(1) == D(1))
00077 return true;
00078 else if ( slopeAB == 0.0 )
00079 {
00080 slopeCD = D(0) - C(0);
00081 if ( slopeCD == 0.0 )
00082 return false;
00083 slopeCD = (D(1) - C(1)) / slopeCD;
00084 T y = slopeCD * (A(0) - C(0)) + C(1);
00085 return ( ( (y > A(1) && y <= B(1)) || (y < A(1) && y >= B(1))
00086 || (y == A(1) && y == B(1)) )
00087 && ( (C(0) > A(0) && D(0) <= A(0))
00088 || (C(0) < A(0) && D(0) >= A(0)) ) );
00089 }
00090 else
00091 {
00092 slopeAB = (B(1) - A(1)) / slopeAB;
00093 slopeCD = D(0) - C(0);
00094 if ( slopeCD == 0.0 )
00095 {
00096 T y = slopeAB * (C(0) - A(0)) + A(1);
00097 return ( ( (y > C(1) && y <= D(1)) || (y < C(1) && y >= D(1))
00098 || (y == C(1) && y == D(1)) )
00099 && ( (D(0) > A(0) && D(0) <= B(0))
00100 || (D(0) < A(0) && D(0) >= B(0)) ) );
00101 }
00102 else
00103 {
00104 slopeCD = (D(1) - C(1)) / slopeCD;
00105 if ( slopeAB - slopeCD == 0.0 )
00106 return false;
00107 T x = ( A(1) - slopeAB * A(0) - C(1) + slopeCD * C(0) )
00108 / (slopeCD - slopeAB);
00109 return ( ( (x > C(0) && x <= D(0)) || (x < C(0) && x >= D(0))
00110 || (x == C(0) && x == D(0)) )
00111 && ( (x > A(0) && x <= B(0))
00112 || (x < A(0) && x >= B(0)) ) );
00113 }
00114 }
00115
00116 }
00117
00118
00119
00121
00123
00124
00125 template <class T>
00126 class Curve: public SpaceTown
00127 {
00128
00129
00130
00131
00132
00133 public:
00134 typedef T value_type;
00135 typedef T* pointer;
00136 typedef const T* const_pointer;
00137 typedef T& reference;
00138 typedef const T& const_reference;
00139
00140
00141
00142
00143
00144
00145 protected:
00147 int n_;
00149 List<Vector<T> > points_;
00152 int orientation_;
00153
00154
00155
00156
00157
00158
00159 public:
00160
00161 Curve() throw();
00162
00163
00164 ~Curve() throw();
00165
00166
00167 void InitWithUnsortedPoints_ClosestPointMethod(List<Vector<T> >& points);
00168 void AddPoint(Vector<T>& point);
00169
00170 void Copy(Curve<T>& C);
00171
00172 int GetNbPoints() const;
00173 List<Vector<T> >& GetPoints();
00174
00175 int GetOrientation() const;
00176 int SetOrientation(int orientation);
00177
00178 void ClearAll();
00179
00180 T GetDistanceToTheCurve(T x, T y);
00181 T GetProjection(Vector<T>& X);
00182 T GetSignedDistanceToTheCurve(T x, T y);
00183
00184
00185 void WriteText(ofstream& f);
00186 void ReadText(string file_name, int orientation = -1);
00187
00188 };
00189
00190
00191 }
00192
00193
00194 #define FILE_CURVE_HXX
00195 #endif