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

Generated on Thu Mar 7 00:37:42 2002 for UvA Trilearn 2001 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001