linesegment2d.cpp
Go to the documentation of this file.
2 
3 using namespace tuw;
6  : Line2D ( l.line() )
7  , p0_ ( l.p0() )
8  , p1_ ( l.p1() ) {
9 };
11  : Line2D ( p0, p1, true )
12  , p0_ ( p0 )
13  , p1_ ( p1 ) {
14 };
15 LineSegment2D::LineSegment2D ( const double &x0, const double &y0, const double &x1, const double &y1 )
16  : Line2D ( x0, y0, x1, y1, true )
17  , p0_ ( x0, y0 )
18  , p1_ ( x1, y1 ) {
19 };
20 const double &LineSegment2D::x0() const {
21  return p0_.x();
22 }
23 const double &LineSegment2D::y0() const {
24  return p0_.y();
25 }
26 const double &LineSegment2D::x1() const {
27  return p1_.x();
28 }
29 const double &LineSegment2D::y1() const {
30  return p1_.y();
31 }
32 double LineSegment2D::angle() const {
33  double dx = p1_.x() - p0_.x();
34  double dy = p1_.y() - p0_.y();
35  return atan2 ( dy,dx );
36 }
38  double dx = p1_.x() - p0_.x();
39  double dy = p1_.y() - p0_.y();
40  return Point2D ( p0_.x() + dx/2., p0_.y() + dy/2. );
41 }
42 const Point2D &LineSegment2D::p0() const {
43  return p0_;
44 }
45 const Point2D &LineSegment2D::p1() const {
46  return p1_;
47 }
48 const Line2D &LineSegment2D::line() const {
49  return *this;
50 }
51 const double LineSegment2D::length() const {
52  return p0_.distanceTo ( p1_ );
53 }
56  return p0() == o.p0() && p1() == o.p1();
57 }
58 LineSegment2D& LineSegment2D::set ( const double &x0, const double &y0, const double &x1, const double &y1 ) {
59  Line2D::set ( x0, y0, x1, y1, true );
60  p0_.set ( x0, y0 ), p1_.set ( x1,y1 );
61  return *this;
62 }
64  set ( p0.x(), p0.y(), p1.x(), p1.y() );
65  return *this;
66 }
73 double LineSegment2D::distanceTo ( const Point2D &p, double &dx, double &dy ) const {
74  return sqrt( distanceSqrTo(p, dx, dy) );
75 }
82 double LineSegment2D::distanceSqrTo ( const Point2D &p, double &dx, double &dy ) const {
83  const double px = x1()-x0();
84  const double py = y1()-y0();
85  const double l2 = px*px + py*py;
86  double u = ( ( p.x() - x0() ) * px + ( p.y() - y0() ) * py ) / l2;
87  if ( u > 1 ) u = 1;
88  else if ( u < 0 ) u = 0;
89  const double xk = x0() + u * px;
90  const double yk = y0() + u * py;
91  dx = xk - p.x();
92  dy = yk - p.y();
93  return dx*dx + dy*dy ;
94 }
95 
97  double px = x1()-x0();
98  double py = y1()-y0();
99  double l2 = px*px + py*py;
100  double u = ((p.x() - x0()) * px + (p.y() - y0()) * py) / l2;
101  if (u > 1){ u = 1; }
102  else if (u < 0){ u = 0; }
103  return u;
104 }
106  double px = x1()-x0();
107  double py = y1()-y0();
108  const double u = closestPointLineSegmentRatio(p);
109  return Point2D(x0() + u * px, y0() + u * py);
110 }
111 
116 double LineSegment2D::distanceTo ( const Point2D &p ) const {
117  double dx, dy;
118  distanceTo ( p, dx, dy );
119 }
120 
const double & y() const
Definition: point2d.cpp:49
double closestPointLineSegmentRatio(const Point2D &p) const
const double & y1() const
const double & x1() const
double distanceTo(const Point2D &p) const
Definition: point2d.cpp:127
bool operator==(const LineSegment2D &o) const
comparison operator
LineSegment2D()
the lines endpoints
const double length() const
Line2D & set(const double &x0, const double &y0, const double &x1, const double &y1, bool normalize=true)
Definition: line2d.cpp:64
Definition: command.h:8
const Line2D & line() const
double angle() const
Point2D closestPointTo(const Point2D &p) const
const double & y0() const
Point2D pc() const
double distanceSqrTo(const Point2D &p, double &dx, double &dy) const
LineSegment2D & set(const double &x0, const double &y0, const double &x1, const double &y1)
const Point2D & p0() const
double distanceTo(const Point2D &p, double &dx, double &dy) const
const double & x() const
Definition: point2d.cpp:35
const double & x0() const
Point2D & set(double x, double y)
Definition: point2d.cpp:17
const Point2D & p1() const


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:08