00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00252
00253
00254
00255 bool onSegment( const Vector2D & p ) const;
00256 };
00257
00258 }
00259
00260 #endif