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
00051 #ifndef _GEOMETRY_
00052 #define _GEOMETRY_
00053
00054 #include "math.h"
00055 #include <string>
00056 #include <iostream>
00057
00058 using namespace std;
00059
00060 typedef double AngRad;
00061 typedef double AngDeg;
00063 #define EPSILON 0.0001
00065 // auxiliary numeric functions for determining the
00066
00067 double max ( double d1, double d2 );
00068 double min ( double d1, double d2 );
00069 int sign ( double d1 );
00070
00071
00072
00073 AngDeg Rad2Deg ( AngRad x );
00074 AngRad Deg2Rad ( AngDeg x );
00075 double cosDeg ( AngDeg x );
00076 double sinDeg ( AngDeg x );
00077 double tanDeg ( AngDeg x );
00078 AngDeg atanDeg ( double x );
00079 double atan2Deg( double x, double y );
00080 AngDeg acosDeg ( double x );
00081 AngDeg asinDeg ( double x );
00082
00083
00084 bool isAngInInterval ( AngDeg ang, AngDeg angMin, AngDeg angMax );
00085 AngDeg getBisectorTwoAngles( AngDeg angMin, AngDeg angMax );
00086
00093 enum CoordSystemT {
00094 CARTESIAN,
00095 POLAR
00096 };
00097
00098
00099
00100
00101
00108 class VecPosition
00109 {
00110
00111 private:
00112
00113 double m_x;
00114 double m_y;
00116
00117 public:
00118
00119 VecPosition ( double vx = 0,
00120 double vy = 0,
00121 CoordSystemT cs =CARTESIAN);
00122
00123
00124 VecPosition operator - ( );
00125 VecPosition operator + ( const double &d );
00126 VecPosition operator + ( const VecPosition &p );
00127 VecPosition operator - ( const double &d );
00128 VecPosition operator - ( const VecPosition &p );
00129 VecPosition operator * ( const double &d );
00130 VecPosition operator * ( const VecPosition &p );
00131 VecPosition operator / ( const double &d );
00132 VecPosition operator / ( const VecPosition &p );
00133 void operator = ( const double &d );
00134 void operator += ( const VecPosition &p );
00135 void operator += ( const double &d );
00136 void operator -= ( const VecPosition &p );
00137 void operator -= ( const double &d );
00138 void operator *= ( const VecPosition &p );
00139 void operator *= ( const double &d );
00140 void operator /= ( const VecPosition &p );
00141 void operator /= ( const double &d );
00142 bool operator != ( const VecPosition &p );
00143 bool operator != ( const double &d );
00144 bool operator == ( const VecPosition &p );
00145 bool operator == ( const double &d );
00146
00147
00148 friend ostream& operator << ( ostream &os,
00149 VecPosition p );
00150 void show ( CoordSystemT cs =CARTESIAN);
00151 string str ( CoordSystemT cs =CARTESIAN);
00152
00153
00154 bool setX ( double dX );
00155 double getX ( ) const;
00156 bool setY ( double dY );
00157 double getY ( ) const;
00158
00159
00160 void setVecPosition ( double dX = 0,
00161 double dY = 0,
00162 CoordSystemT cs =CARTESIAN);
00163 double getDistanceTo ( const VecPosition p );
00164 VecPosition setMagnitude ( double d );
00165 double getMagnitude ( ) const;
00166 AngDeg getDirection ( ) const;
00167
00168
00169 bool isInFrontOf ( const VecPosition &p );
00170 bool isInFrontOf ( const double &d );
00171 bool isBehindOf ( const VecPosition &p );
00172 bool isBehindOf ( const double &d );
00173 bool isLeftOf ( const VecPosition &p );
00174 bool isLeftOf ( const double &d );
00175 bool isRightOf ( const VecPosition &p );
00176 bool isRightOf ( const double &d );
00177 bool isBetweenX ( const VecPosition &p1,
00178 const VecPosition &p2 );
00179 bool isBetweenX ( const double &d1,
00180 const double &d2 );
00181 bool isBetweenY ( const VecPosition &p1,
00182 const VecPosition &p2 );
00183 bool isBetweenY ( const double &d1,
00184 const double &d2 );
00185
00186
00187 VecPosition normalize ( );
00188 VecPosition rotate ( AngDeg angle );
00189 VecPosition globalToRelative ( VecPosition orig,
00190 AngDeg ang );
00191 VecPosition relativeToGlobal ( VecPosition orig,
00192 AngDeg ang );
00193 VecPosition getVecPositionOnLineFraction( VecPosition &p,
00194 double dFrac );
00195
00196
00197 static VecPosition getVecPositionFromPolar( double dMag,
00198 AngDeg ang );
00199 static AngDeg normalizeAngle ( AngDeg angle );
00200 };
00201
00202
00203
00204
00205
00207 class Geometry
00208 {
00209
00210 public:
00211
00212
00213 static double getLengthGeomSeries(double dFirst,double dRatio,double dSum );
00214 static double getSumGeomSeries (double dFirst,double dRatio,double dLen );
00215 static double getSumInfGeomSeries(double dFirst,double dRatio );
00216 static double getFirstGeomSeries (double dSum, double dRatio,double dLen );
00217 static double getFirstInfGeomSeries(double dSum,double dRatio );
00218
00219
00220 static int abcFormula(double a,double b, double c, double *s1,double *s2);
00221 };
00222
00223
00224
00225
00226
00229 class Circle
00230 {
00231 VecPosition m_posCenter;
00232 double m_dRadius;
00234 public:
00235 Circle( );
00236 Circle( VecPosition pos, double dR );
00237
00238 void show ( ostream& os = cout );
00239
00240
00241 bool setCircle ( VecPosition pos,
00242 double dR );
00243 bool setRadius ( double dR );
00244 double getRadius ( );
00245 bool setCenter ( VecPosition pos );
00246 VecPosition getCenter ( );
00247 double getCircumference ( );
00248 double getArea ( );
00249
00250
00251 bool isInside ( VecPosition pos );
00252 int getIntersectionPoints ( Circle c,
00253 VecPosition *p1,
00254 VecPosition *p2 );
00255 double getIntersectionArea ( Circle c );
00256
00257
00258 } ;
00259
00260
00261
00262
00263
00267 class Line
00268 {
00269
00270 double m_a;
00271 double m_b;
00272 double m_c;
00274 public:
00275 Line( double a, double b, double c );
00276
00277
00278 void show( ostream& os = cout );
00279 friend ostream& operator << (ostream & os, Line l);
00280
00281
00282 VecPosition getIntersection ( Line line );
00283 int getCircleIntersectionPoints( Circle circle,
00284 VecPosition *posSolution1,
00285 VecPosition *posSolution2 );
00286 Line getTangentLine ( VecPosition pos );
00287 VecPosition getPointOnLineClosestTo ( VecPosition pos );
00288 double getDistanceWithPoint ( VecPosition pos );
00289 bool isInBetween ( VecPosition pos,
00290 VecPosition point1,
00291 VecPosition point2 );
00292
00293
00294 double getYGivenX ( double x );
00295 double getXGivenY ( double y );
00296 double getACoefficient ( ) const;
00297 double getBCoefficient ( ) const;
00298 double getCCoefficient ( ) const;
00299
00300
00301 static Line makeLineFromTwoPoints ( VecPosition pos1,
00302 VecPosition pos2 );
00303 static Line makeLineFromPositionAndAngle( VecPosition vec,
00304 AngDeg angle );
00305 };
00306
00307
00308
00309
00313 class Rect
00314 {
00315 VecPosition m_posLeftTop;
00316 VecPosition m_posRightBottom;
00318 public:
00319 Rect ( VecPosition pos, VecPosition pos2 );
00320
00321 void show ( ostream& os = cout );
00322
00323
00324 bool isInside ( VecPosition pos );
00325
00326
00327 void setRectanglePoints( VecPosition pos1,
00328 VecPosition pos2 );
00329 bool setPosLeftTop ( VecPosition pos );
00330 VecPosition getPosLeftTop ( );
00331 bool setPosRightBottom ( VecPosition pos );
00332 VecPosition getPosRightBottom ( );
00333 };
00334
00335 #endif