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

  1. // point.cpp
  2.  
  3. // Copyright (C) 1997  Cliff Johnson                                       //
  4. //                                                                         //
  5. // This program is free software; you can redistribute it and/or           //
  6. // modify it under the terms of the GNU  General Public                    //
  7. // License as published by the Free Software Foundation; either            //
  8. // version 2 of the License, or (at your option) any later version.        //
  9. //                                                                         //
  10. // This software is distributed in the hope that it will be useful,        //
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of          //
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
  13. // General Public License for more details.                                //
  14. //                                                                         //
  15. // You should have received a copy of the GNU General Public License       //
  16. // along with this software (see COPYING.LIB); if not, write to the        //
  17. // Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
  18.  
  19. #include "point.h"        //    THIS
  20. #include "iostream.h"        //    ITO
  21. #include "math.h"        //    ITO
  22. #include "geom_enum.h"
  23. #include "geomexception.h"
  24.  
  25.  
  26. double Point::NullDist =  0.0005;
  27. double Point::NullAngle = 0.0001;
  28. double Point::MaxSize =  50000.0; 
  29.  
  30. //-------------------------------------------------------------
  31. //    Constructors
  32. //-------------------------------------------------------------
  33.  
  34. Point::Point(): Geom(POINT)
  35. {
  36.     P[0]=P[1]=P[2]=0.;
  37. }
  38.  
  39. //-------------------------------------------------------------
  40. Point::Point(double* p): Geom(POINT)
  41. {
  42.     for(int i=0;i<3;i++)P[i]=p[i];
  43. }
  44. //-------------------------------------------------------------
  45. // copy constructor
  46. Point::Point(const Point& p): Geom(POINT)
  47. {
  48.     for(int i=0;i<3;i++)P[i]=p.P[i];
  49. }
  50.  
  51. //-------------------------------------------------------------
  52. Point::Point(double x, double y, double z): Geom(POINT)
  53. {
  54.     P[0]=x;
  55.     P[1]=y;
  56.     P[2]=z;
  57. }
  58.  
  59. //-------------------------------------------------------------
  60. //    Methods
  61. //-------------------------------------------------------------
  62. // assignment operator 
  63. Point& Point::operator=(const Point& p)
  64. {
  65.     for(int i=0;i<3;i++)P[i]=p[i];
  66.     return *this;
  67. }
  68.  
  69. //-------------------------------------------------------------
  70. istream& operator>>(istream& s , Point& p)
  71. {
  72.     s >> p.P[0] >> p.P[1] >> p.P[2];
  73.     return s;
  74. }
  75. //-------------------------------------------------------------
  76.  
  77. ostream& operator<<(ostream& s , const Point& p)
  78. {
  79.   s << p[0] << ' ' << p[1] << ' ' << p[2] ;
  80.     return s;
  81. }
  82. //-------------------------------------------------------------
  83. double Point::Magnitude() const
  84. {
  85.     return sqrt(pow(P[0],2)+pow(P[1],2)+pow(P[2],2));
  86. }
  87. //-------------------------------------------------------------
  88. Point Point::Normal() const throw (GeomException)
  89. {
  90.     double m = Magnitude();
  91.     if(m < NullDist)throw GeomException("Point::Normal() : null vector cannot be normalized");
  92.     return Point((*this) * (1/m));
  93. }
  94. //-------------------------------------------------------------
  95. Point Point::Round(int power) const
  96. {
  97.     double p = pow(10,power);
  98.     return Point(
  99.         p * rint( P[0]/p ),
  100.             p * rint( P[1]/p ),
  101.             p * rint( P[2]/p )
  102.     );
  103. }
  104. //-------------------------------------------------------------
  105.  
  106. Point Point::operator+(const Point& p) const
  107. {
  108.     double px[3];
  109.     for(int i=0;i<3;i++)px[i]=P[i]+p[i];
  110.     return Point(px);
  111. }
  112.  
  113. //-------------------------------------------------------------
  114. Point Point::operator-(const Point& p) const
  115. {
  116.     double px[3];
  117.     for(int i=0;i<3;i++)px[i]=P[i]-p[i];
  118.     return Point(px);
  119. }
  120. //-------------------------------------------------------------
  121.  
  122. double Point::operator*(const Point& p) const // dot product:
  123. {
  124.     return P[0]*p[0] + P[1]*p[1] + P[2]*p[2];
  125. }
  126.  
  127. //-------------------------------------------------------------
  128. Point operator-(const Point& p1)
  129. {
  130.     double p[3];
  131.     for(int i=0;i<3;i++)p[i]=-p1[i];
  132.     return Point(p);
  133. }
  134.  
  135. //-------------------------------------------------------------
  136. bool operator==(const Point& p1, const Point& p2)// Coordinates are equal
  137. {
  138.     return( p1.Distance(p2) < Point::NullDist);
  139. }
  140. //-------------------------------------------------------------
  141. //Note two forms of this following:
  142. // Member
  143.  
  144. Point Point::operator*(double ratio) const // return a vector scaled from this one.
  145. {
  146.     double px[3];
  147.     for(int i=0;i<3;i++)px[i]=P[i]*ratio;
  148.     return Point(px);
  149. }
  150. // friend
  151. Point operator*(double ratio, const Point& p)    // return a vector scaled from this one.
  152. {
  153.     double px[3];
  154.     for(int i=0;i<3;i++)px[i]=p[i]*ratio;
  155.     return Point(px);
  156. }
  157. //-------------------------------------------------------------
  158.  
  159. Point Cross(const Point& p1, const Point& p2) // cross product
  160. {
  161.     double p[3];
  162.     p[0]=p1[1]*p2[2]-p1[2]*p2[1];
  163.     p[1]=p1[2]*p2[0]-p1[0]*p2[2];
  164.     p[2]=p1[0]*p2[1]-p1[1]*p2[0];
  165.     return Point(p);
  166. }
  167. //-------------------------------------------------------------
  168.  
  169. double Angle(const Point& v1, const Point& v2) throw (GeomException)
  170. {
  171.     Point vx1,vx2;
  172. //    double l1,l2,a;
  173.     double dot,angle;
  174.  
  175.     try
  176.     {
  177.         vx1 = v1.Normal();
  178.         vx2 = v2.Normal();
  179.     }
  180.     catch (GeomException& ge)
  181.     {
  182.         throw;
  183.     }
  184.     dot = vx1 * vx2;
  185.  
  186.     if(dot >= 1 ) angle = 0;
  187.     else if(dot <= -1 ) angle = M_PI;
  188.     else angle = acos(dot);
  189.     return angle;
  190. }
  191. //-------------------------------------------------------------
  192.  
  193. double Distance(const Point& p1, const Point& p2)
  194. {
  195.     return p1.Distance(p2);
  196. }
  197.  
  198. //-------------------------------------------------------------
  199. double Point::Distance(const Point& p) const
  200. {
  201.     Point pcopy(*this);
  202.     Point px = p - pcopy;
  203.     return px.Magnitude();
  204. }
  205.  
  206.  
  207. //-------------------------------------------------------------
  208.  
  209.  
  210.