Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Geometry.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice, this
00009 list of conditions and the following disclaimer.
00010 
00011 2. Redistributions in binary form must reproduce the above copyright notice,
00012 this list of conditions and the following disclaimer in the documentation
00013 and/or other materials provided with the distribution.
00014 
00015 3. Neither the name of the University of Amsterdam nor the names of its
00016 contributors may be used to endorse or promote products derived from this
00017 software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 */
00030 
00051 #ifndef _GEOMETRY_
00052 #define _GEOMETRY_
00053 
00054 #include "math.h"       // needed for M_PI constant
00055 #include <string>       // needed for 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 // maximum and minimum of two given double values and the sign of a value
00067 double max     ( double d1, double d2 );
00068 double min     ( double d1, double d2 );
00069 int    sign    ( double d1            );
00070 
00071 // auxiliary goniometric functions which enable you to
00072 // specify angles in degrees rather than in radians
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 // various goniometric functions
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 /********************   CLASS VECPOSITION   **********************************/
00100 /*****************************************************************************/
00101 
00108 class VecPosition
00109 {
00110   // private member data
00111 private:
00112 
00113   double m_x;   
00114   double m_y;   
00116   // public methods
00117 public:
00118   // constructor for VecPosition class
00119   VecPosition                               ( double            vx = 0,
00120                                               double            vy = 0,
00121                                               CoordSystemT      cs =CARTESIAN);
00122 
00123   // overloaded arithmetic operators
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   // methods for producing output
00148   friend ostream&    operator <<            ( ostream           &os,
00149                                               VecPosition       p            );
00150   void               show                   ( CoordSystemT      cs =CARTESIAN);
00151   string             str                    ( CoordSystemT      cs =CARTESIAN);
00152 
00153   // set- and get methods for private member variables
00154   bool               setX                   ( double            dX           );
00155   double             getX                   (                          ) const;
00156   bool               setY                   ( double            dY           );
00157   double             getY                   (                          ) const;
00158 
00159   // set- and get methods for derived position information
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   // comparison methods for positions
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   // conversion methods for positions
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   // static class methods
00197   static VecPosition getVecPositionFromPolar( double            dMag,
00198                                               AngDeg            ang          );
00199   static AngDeg      normalizeAngle         ( AngDeg            angle        );
00200 };
00201 
00202 /*****************************************************************************/
00203 /*********************   CLASS GEOMETRY   ************************************/
00204 /*****************************************************************************/
00205 
00207 class Geometry
00208 {
00209 
00210 public:
00211 
00212   // geometric series
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   // abc formula
00220   static int    abcFormula(double a,double b, double c, double *s1,double *s2);
00221 };
00222 
00223 /*****************************************************************************/
00224 /********************** CLASS CIRCLE *****************************************/
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     // get and set methods
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     // calculate intersection points and area with other circle
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 /*********************** CLASS LINE ******************************************/
00262 /*****************************************************************************/
00263 
00267 class Line
00268 {
00269   // a line is defined by the formula: ay + bx + c = 0
00270   double m_a; 
00271   double m_b; 
00272   double m_c; 
00274 public:
00275   Line( double a, double b, double c );
00276 
00277   // print methods
00278   void        show( ostream& os = cout );
00279   friend      ostream& operator << (ostream & os, Line l);
00280 
00281   // get intersection points with this line
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   // calculate associated variables in the line
00294   double      getYGivenX                 ( double      x );
00295   double      getXGivenY                 ( double      y );
00296   double      getACoefficient            (               ) const;
00297   double      getBCoefficient            (               ) const;
00298   double      getCCoefficient            (               ) const;
00299 
00300   // static methods to make a line using an easier representation.
00301   static Line makeLineFromTwoPoints      ( VecPosition pos1,
00302                                            VecPosition pos2                  );
00303   static Line makeLineFromPositionAndAngle( VecPosition vec,
00304                                            AngDeg angle                      );
00305 };
00306 
00307 /*****************************************************************************/
00308 /********************** CLASS RECTANGLE **************************************/
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   // checks whether point lies inside the rectangle
00324   bool        isInside          ( VecPosition pos                   );
00325 
00326   // standard get and set methosd
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

Generated on Fri Nov 7 11:45:39 2003 for UvA Trilearn 2003 Base Code by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001