segment_2d.h

説明を見る。
00001 // -*-c++-*-
00002 
00008 /*
00009  *Copyright:
00010 
00011  Copyright (C) Hidehisa Akiyama, Hiroki Shimora
00012 
00013  This code is free software; you can redistribute it and/or
00014  modify it under the terms of the GNU Lesser General Public
00015  License as published by the Free Software Foundation; either
00016  version 2.1 of the License, or (at your option) any later version.
00017 
00018  This library is distributed in the hope that it will be useful,
00019  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  Lesser General Public License for more details.
00022 
00023  You should have received a copy of the GNU Lesser General Public
00024  License along with this library; if not, write to the Free Software
00025  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027  *EndCopyright:
00028  */
00029 
00031 
00032 #ifndef RCSC_GEOM_SEGMENT2D_H
00033 #define RCSC_GEOM_SEGMENT2D_H
00034 
00035 #include <rcsc/geom/line_2d.h>
00036 #include <rcsc/geom/vector_2d.h>
00037 
00038 #include <cmath>
00039 
00040 namespace rcsc {
00041 
00046 class Segment2D {
00047 private:
00048 
00049     Vector2D M_a; 
00050     Vector2D M_b; 
00051 
00053     Segment2D();
00054 
00055     bool checkIntersectsOnLine( const Vector2D & p ) const;
00056 
00057 public:
00063     Segment2D( const Vector2D & a,
00064                const Vector2D & b )
00065         : M_a( a )
00066         , M_b( b )
00067       { }
00068 
00076     Segment2D( const double & ax,
00077                const double & ay,
00078                const double & bx,
00079                const double & by )
00080         : M_a( ax, ay )
00081         , M_b( bx, by )
00082       { }
00083 
00090     const
00091     Segment2D & assign( const Vector2D & a,
00092                         const Vector2D & b )
00093       {
00094           M_a = a;
00095           M_b = b;
00096           return *this;
00097       }
00098 
00106     const
00107     Segment2D & assign( const double & ax,
00108                         const double & ay,
00109                         const double & bx,
00110                         const double & by )
00111       {
00112           M_a.assign( ax, ay );
00113           M_b.assign( bx, by );
00114           return *this;
00115       }
00116 
00121     const
00122     Segment2D & swap()
00123       {
00124           // std::swap( M_a, M_b );
00125           rcsc::Vector2D tmp = M_a;
00126           M_a = M_b;
00127           M_b = tmp;
00128           return *this;
00129       }
00130 
00135     const
00136     Vector2D & a() const
00137       {
00138           return M_a;
00139       }
00140 
00145     const
00146     Vector2D & b() const
00147       {
00148           return M_b;
00149       }
00150 
00155     Line2D line() const
00156       {
00157           return Line2D( a(), b() );
00158       }
00159 
00164     double length() const
00165       {
00166           return a().dist( b() );
00167       }
00168 
00173     Line2D perpendicularBisector() const
00174       {
00175           return Line2D::perpendicular_bisector( a(), b() );
00176       }
00177 
00183     bool contains( const Vector2D & p ) const
00184       {
00185           return ( ( p.x - a().x ) * ( p.x - b().x ) <= 1.0e-5
00186                    && ( p.y - a().y ) * ( p.y - b().y ) <= 1.0e-5 );
00187       }
00188 
00195     Vector2D intersection( const Segment2D & other ) const;
00196 
00203     Vector2D intersection( const Line2D & other ) const;
00204 
00210     bool existIntersection( const Segment2D & other ) const;
00211 
00219     bool existIntersectionExceptEndpoint( const Segment2D & other ) const;
00220 
00227     Vector2D nearestPoint( const Vector2D & p ) const;
00228 
00234     double dist( const Vector2D & p ) const;
00235 
00241     double dist( const Segment2D & seg ) const;
00242 
00248     double farthestDist( const Vector2D & p ) const;
00249 
00250     /*
00251       \brief check point is on segment or not
00252       \p point to check
00253       \p return true if point is on this segment
00254     */
00255     bool onSegment( const Vector2D & p ) const;
00256 };
00257 
00258 }
00259 
00260 #endif

librcscに対してThu May 1 15:41:20 2008に生成されました。  doxygen 1.5.0