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

  1. // line.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 "line.h"
  20. #include "geom_enum.h"
  21. #include "geomexception.h"
  22.  
  23.  
  24. Line::Line():Geom(LINE),origin(),direction(1,0,0)
  25. {
  26. }
  27.  
  28. Line::Line(const Point& o, const Point& d) throw (GeomException):Geom(LINE),origin(o)
  29. {
  30.     if(d == Point())throw GeomException("Line::Line(Point,Dir) : null direction");
  31.     direction = d.Normal();
  32. }
  33.  
  34. ostream& operator<<(ostream& os, const Line& l)
  35. {
  36.     os << "Line: P = " << l.origin << "  D =  " << l.direction;
  37.     return os;
  38. }
  39.  
  40. Point Line::Project(const Point& p) const
  41. {
  42.     Point px, vp;
  43.     if(p.Distance(origin)< Point::NullDist)return p; // point is at origin 
  44.     vp = p - origin;
  45.     double theta;
  46.     try
  47.     {
  48.         theta = Angle(direction,vp);
  49.     }
  50.     catch (GeomException& ge)
  51.     {    // point on line
  52.         return p;
  53.     }
  54.     px = origin + ( vp.Magnitude() * cos(theta))* direction ;
  55.  
  56.     return px;
  57. }
  58.  
  59. double Line::Distance(const Point& p ) const
  60. {
  61.     Point px = Project(p);
  62.     return p.Distance(px);
  63. }
  64.