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

Geometry.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2002, 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 */
00050 #ifndef _GEOMETRY_
00051 #define _GEOMETRY_
00052 
00053 #include "math.h"       // needed for M_PI constant
00054 #include <string>       // needed for string
00055 
00056 typedef double AngRad;  
00057 typedef double AngDeg;  
00059 #define EPSILON 0.0001  
00061 // auxiliary numeric functions for determining the
00062 // maximum and minimum of two given double values and the sign of a value
00063 double max     ( double d1, double d2 );
00064 double min     ( double d1, double d2 );
00065 int    sign    ( double d1            );
00066 
00067 // auxiliary goniometric functions which enable you to
00068 // specify angles in degrees rather than in radians
00069 AngDeg Rad2Deg ( AngRad x             );
00070 AngRad Deg2Rad ( AngDeg x             );
00071 double cosDeg  ( AngDeg x             );
00072 double sinDeg  ( AngDeg x             );
00073 double tanDeg  ( AngDeg x             );
00074 AngDeg atanDeg ( double x             );
00075 double atan2Deg( double x,  double y  );
00076 AngDeg acosDeg ( double x             );
00077 AngDeg asinDeg ( double x             );
00078 
00079 // various goniometric functions
00080 bool   isAngInInterval     ( AngDeg ang,    AngDeg angMin,    AngDeg angMax );
00081 AngDeg getBisectorTwoAngles( AngDeg angMin, AngDeg angMax );
00082 
00088 enum CoordSystemT {
00089   CARTESIAN,
00090   POLAR
00091 };
00092 
00093 /******************************************************************************/
00094 /********************   CLASS VECPOSITION   ***********************************/
00095 /******************************************************************************/
00096 
00103 class VecPosition
00104 {
00105   // private member data
00106 private:
00107 
00108   double m_x;   
00109   double m_y;   
00111   // public methods
00112 public:
00113   // constructor for VecPosition class
00114   VecPosition                               ( double            vx = 0,
00115                                               double            vy = 0,
00116                                               CoordSystemT      cs = CARTESIAN);
00117 
00118   // overloaded arithmetic operators
00119   VecPosition        operator -             (                                 );
00120   VecPosition        operator +             ( const double      &d            );
00121   VecPosition        operator +             ( const VecPosition &p            );
00122   VecPosition        operator -             ( const double      &d            );
00123   VecPosition        operator -             ( const VecPosition &p            );
00124   VecPosition        operator *             ( const double      &d            );
00125   VecPosition        operator *             ( const VecPosition &p            );
00126   VecPosition        operator /             ( const double      &d            );
00127   VecPosition        operator /             ( const VecPosition &p            );
00128   void               operator =             ( const double      &d            );
00129   void               operator +=            ( const VecPosition &p            );
00130   void               operator +=            ( const double      &d            );
00131   void               operator -=            ( const VecPosition &p            );
00132   void               operator -=            ( const double      &d            );
00133   void               operator *=            ( const VecPosition &p            );
00134   void               operator *=            ( const double      &d            );
00135   void               operator /=            ( const VecPosition &p            );
00136   void               operator /=            ( const double      &d            );
00137   bool               operator !=            ( const VecPosition &p            );
00138   bool               operator !=            ( const double      &d            );
00139   bool               operator ==            ( const VecPosition &p            );
00140   bool               operator ==            ( const double      &d            );
00141 
00142   // methods for producing output
00143   friend ostream&    operator <<            ( ostream           &os,
00144                                               VecPosition       p             );
00145   void               show                   ( CoordSystemT      cs = CARTESIAN);
00146   string             str                    ( CoordSystemT      cs = CARTESIAN);
00147 
00148   // set- and get methods for private member variables
00149   bool               setX                   ( double            dX            );
00150   double             getX                   (                           ) const;
00151   bool               setY                   ( double            dY            );
00152   double             getY                   (                           ) const;
00153 
00154   // set- and get methods for derived position information
00155   void               setVecPosition         ( double            dX = 0,
00156                                               double            dY = 0,
00157                                               CoordSystemT      cs = CARTESIAN);
00158   double             getDistanceTo          ( const VecPosition p             );
00159   VecPosition        setMagnitude           ( double            d             );
00160   double             getMagnitude           (                           ) const;
00161   AngDeg             getDirection           (                           ) const;
00162 
00163   // comparison methods for positions
00164   bool               isInFrontOf            ( const VecPosition &p            );
00165   bool               isInFrontOf            ( const double      &d            );
00166   bool               isBehindOf             ( const VecPosition &p            );
00167   bool               isBehindOf             ( const double      &d            );
00168   bool               isLeftOf               ( const VecPosition &p            );
00169   bool               isLeftOf               ( const double      &d            );
00170   bool               isRightOf              ( const VecPosition &p            );
00171   bool               isRightOf              ( const double      &d            );
00172   bool               isBetweenX             ( const VecPosition &p1,
00173                                               const VecPosition &p2           );
00174   bool               isBetweenX             ( const double      &d1,
00175                                               const double      &d2           );
00176   bool               isBetweenY             ( const VecPosition &p1,
00177                                               const VecPosition &p2           );
00178   bool               isBetweenY             ( const double      &d1,
00179                                               const double      &d2           );
00180 
00181   // conversion methods for positions
00182   VecPosition        normalize              (                                 );
00183   VecPosition        rotate                 ( AngDeg            angle         );
00184   VecPosition        globalToRelative       ( VecPosition       orig,
00185                                               AngDeg            ang           );
00186   VecPosition        relativeToGlobal       ( VecPosition       orig,
00187                                               AngDeg            ang           );
00188   VecPosition        getVecPositionOnLineFraction( VecPosition  &p,
00189                                               double            dFrac         );
00190 
00191   // static class methods
00192   static VecPosition getVecPositionFromPolar( double            dMag,
00193                                               AngDeg            ang           );
00194   static AngDeg      normalizeAngle         ( AngDeg            angle         );
00195 };
00196 
00197 /******************************************************************************/
00198 /*********************   CLASS GEOMETRY   *************************************/
00199 /******************************************************************************/
00200 
00202 class Geometry
00203 {
00204 
00205 public:
00206 
00207   // geometric series
00208   static double getLengthGeomSeries(double dFirst,double dRatio,double dSum   );
00209   static double getSumGeomSeries   (double dFirst,double dRatio,double dLength);
00210   static double getSumInfGeomSeries(double dFirst,double dRatio               );
00211   static double getFirstGeomSeries (double dSum,  double dRatio,double dLength);
00212   static double getFirstInfGeomSeries(double dSum,double dRatio               );
00213 
00214   // abc formula
00215   static int    abcFormula(double a,double b, double c, double *s1, double *s2);
00216 };
00217 
00218 /******************************************************************************/
00219 /********************** CLASS CIRCLE ******************************************/
00220 /******************************************************************************/
00221 
00224 class Circle
00225 {
00226     VecPosition m_posCenter;            
00227     double      m_dRadius;              
00229 public:
00230     Circle( );
00231     Circle( VecPosition pos, double dR );
00232 
00233     void        show                  ( ostream& os = cout );
00234 
00235     // get and set methods
00236     bool        setCircle             ( VecPosition pos, 
00237                                         double      dR  );
00238     bool        setRadius             ( double dR       );
00239     double      getRadius             (                 );
00240     bool        setCenter             ( VecPosition pos );
00241     VecPosition getCenter             (                 );
00242     double      getCircumference      (                 );
00243     double      getArea               (                 );
00244 
00245     // calculate intersection points and area with other circle
00246     bool        isInside              ( VecPosition pos );
00247     int         getIntersectionPoints ( Circle      c, 
00248                                         VecPosition *p1, 
00249                                         VecPosition *p2 );
00250     double      getIntersectionArea   ( Circle c        );
00251 
00252 
00253 }  ;
00254 
00255 /******************************************************************************/
00256 /*********************** CLASS LINE *******************************************/
00257 /******************************************************************************/
00258 
00262 class Line
00263 {
00264   // a line is defined by the formula: ay + bx + c = 0
00265   double m_a; 
00266   double m_b; 
00267   double m_c; 
00269 public:
00270   Line( double a, double b, double c );
00271 
00272   // print methods
00273   void        show( ostream& os = cout );
00274   friend      ostream& operator << (ostream & os, Line l);
00275 
00276   // get intersection points with this line
00277   VecPosition getIntersection            ( Line        line                   );
00278   int         getCircleIntersectionPoints( Circle      circle,
00279                                            VecPosition *posSolution1,
00280                                            VecPosition *posSolution2          );
00281   Line        getTangentLine             ( VecPosition pos                    );
00282   VecPosition getPointOnLineClosestTo    ( VecPosition pos                    );
00283   double      getDistanceWithPoint       ( VecPosition pos                    );
00284   bool        isInBetween                ( VecPosition pos,
00285                                            VecPosition point1,
00286                                            VecPosition point2                 );
00287 
00288   // calculate associated variables in the line
00289   double getYGivenX                      ( double      x );
00290   double getXGivenY                      ( double      y );
00291   double getACoefficient                 (               ) const;
00292   double getBCoefficient                 (               ) const;
00293   double getCCoefficient                 (               ) const;
00294 
00295   // static methods to make a line using an easier representation.
00296   static Line makeLineFromTwoPoints      ( VecPosition pos1,
00297                                            VecPosition pos2                   );
00298   static Line makeLineFromPositionAndAngle( VecPosition vec,
00299                                            AngDeg angle                       );
00300 };
00301 
00302 /******************************************************************************/
00303 /********************** CLASS RECTANGLE ***************************************/
00304 /******************************************************************************/
00305 
00308 class Rectangle
00309 {
00310   VecPosition m_posLeftTop;     
00311   VecPosition m_posRightBottom; 
00313 public:
00314   Rectangle                     ( VecPosition pos, VecPosition pos2 );
00315 
00316   void        show              ( ostream& os = cout                );
00317 
00318   // checks whether point lies inside the rectangle
00319   bool        isInside          ( VecPosition pos                   );
00320 
00321   // standard get and set methosd
00322   void        setRectanglePoints( VecPosition pos1,
00323                                   VecPosition pos2                  );
00324   bool        setPosLeftTop     ( VecPosition pos                   );
00325   VecPosition getPosLeftTop     ( VecPosition pos                   );
00326   bool        setPosRightBottom ( VecPosition pos                   );
00327   VecPosition getPosRightBottom ( VecPosition pos                   );
00328 };
00329 
00330 #endif

Generated on Tue Jul 2 10:18:52 2002 for UvA Trilearn 2002 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001