home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / freedraft.tar.gz / freedraft.tar / FREEdraft-050298 / GEOMLIB2D / circle.cpp < prev    next >
C/C++ Source or Header  |  1998-04-19  |  4KB  |  144 lines

  1. // circle.cpp
  2.  
  3.  
  4. // Copyright (C) 1997  Cliff Johnson                                       //
  5. //                                                                         //
  6. // This program is free software; you can redistribute it and/or           //
  7. // modify it under the terms of the GNU  General Public                    //
  8. // License as published by the Free Software Foundation; either            //
  9. // version 2 of the License, or (at your option) any later version.        //
  10. //                                                                         //
  11. // This software is distributed in the hope that it will be useful,        //
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of          //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
  14. // General Public License for more details.                                //
  15. //                                                                         //
  16. // You should have received a copy of the GNU General Public License       //
  17. // along with this software (see COPYING.LIB); if not, write to the        //
  18. // Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
  19.  
  20.  
  21. #include "circle.h"
  22. #include "geom_enum.h"
  23. #include "math.h"
  24. #include "geomexception.h"
  25.  
  26.  
  27. // unit circle  - default constructor
  28.  
  29. Circle::Circle():Geom(CIRCLE)        // default is unit radius circle at origin - 
  30. {
  31.     center = Point();
  32.     origin = Point(1,0);
  33. }
  34.  
  35. // Circle by center and origin
  36.  
  37. Circle::Circle(const Point& c, const Point& o) throw (GeomException) :Geom(CIRCLE)
  38. {
  39.     if( c == o ) 
  40.         throw GeomException("Circle::Circle(c,o) : points are coincident");
  41.     center = c;
  42.     origin = o;
  43. }
  44.  
  45. // circle by center and radius
  46.  
  47. Circle::Circle(const Point& c, double r) throw (GeomException) :Geom(CIRCLE)
  48. {
  49.     if(fabs(r) < Point::NullDist)
  50.         throw GeomException("Circle::Circle(c,r) : r is zero");
  51.     center = c;
  52.     origin = c + Point(r,0);
  53. }
  54.  
  55.  
  56. Circle::Circle(const Circle& c):Geom(CIRCLE)
  57. {
  58.     center = c.center;
  59.     origin = c.origin;
  60. }
  61.  
  62. Circle& Circle::operator=(const Circle& c)
  63. {
  64.     center = c.center;
  65.     origin = c.origin;
  66.     return *this;
  67. }
  68.  
  69. double Circle::Radius() const
  70. {
  71.     return center.Distance(origin);
  72. }
  73.  
  74. Point Circle::U(double u) const
  75. {
  76.     if(center == origin) return center;    // check for zero radius;
  77.  
  78.     Point v = origin - center;
  79.     double sine = sin(u * 2 * M_PI);
  80.     double cosine = cos(u * 2 * M_PI);
  81.  
  82.     return center + Point( v[0]*cosine - v[1]*sine, v[0]*sine + v[1]*cosine);
  83. }
  84.  
  85. ostream& operator<<(ostream& os, const Circle& c)
  86. {
  87.     
  88.     os << "Circle: center= " << c.center << " origin= " << c.origin;
  89.     return os;
  90. }
  91.  
  92. Point Circle::Project(const Point& p ) const throw (GeomException)
  93. {
  94. //    if(center == origin) return center;     // Null circles ( not allowed anymore ) 
  95.     if(p == center) throw GeomException("Circle::Project : Point is at Circle center");
  96.  
  97.     // vector from center to point
  98.     Point v = p - center;
  99.  
  100.     return center + ( v.Normal() * Radius() ) ;     // the exception above should be 
  101.                             //thrown instead of this one
  102. }
  103.  
  104. double Circle::Distance(const Point& p ) const
  105. {
  106.     // if(center == origin) return center.Distance(p);
  107.     if(p == center) return Radius();
  108.  
  109.     // vector from center to point
  110.     Vector v = p - center;
  111.  
  112.     Point px = center + ( v.Normal() * Radius() ) ;    // ~should~ not be null :)
  113.  
  114.  
  115.     return p.Distance(px);
  116. }
  117.  
  118.     
  119.  
  120. double Circle::UProject(const Point& p ) const throw (GeomException)
  121. {
  122.     //if(center == origin) return 0;     // Null circle
  123.     if(p == center) throw GeomException("Circle::UProject() : Point at Circle center");
  124.  
  125.     // vector from center to origin
  126.     Point v0 = origin - center;
  127.  
  128.     // vector from center to point
  129.     Point v1 = p - center;
  130.  
  131.     //get angle
  132.     double theta = Angle(v0, v1);    // both vectors should not be null
  133.                     // here, so no worry about raising exception :)
  134.     // get cross product
  135.     Point px = Cross(v0,v1);
  136.  
  137.     // if left hand rotation, theta is between PI and 2*PI
  138.     if(px[2]<0)theta = 2 * M_PI - theta;
  139.  
  140.     return (theta / (2 * M_PI));
  141. }
  142.  
  143.  
  144.