31 #ifndef VPSC_LINESEGMENT_H 32 #define VPSC_LINESEGMENT_H 54 : begin_(begin), end_(end) {}
56 enum IntersectResult { PARALLEL, COINCIDENT, NOT_INTERSECTING, INTERSECTING };
58 IntersectResult Intersect(
const LineSegment& other_line,
Vector& intersection)
60 double dx1=end_.x_ - begin_.x_;
61 double dy1=end_.y_ - begin_.y_;
62 double dx2=other_line.end_.x_ - other_line.begin_.x_;
63 double dy2=other_line.end_.y_ - other_line.begin_.y_;
65 double denom = dy2 * dx1 - dy1 * dx2;
67 double nume_a = dx2 * (begin_.y_ - other_line.begin_.y_) -
68 dy2 * (begin_.x_ - other_line.begin_.x_);
70 double nume_b = dx1 * (begin_.y_ - other_line.begin_.y_) -
71 dy1 * (begin_.x_ - other_line.begin_.x_);
75 if(nume_a == 0.0f && nume_b == 0.0f)
82 double ua = nume_a / denom;
83 double ub = nume_b / denom;
85 if(ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f)
88 intersection.x_ = begin_.x_ + ua*dx1;
89 intersection.y_ = begin_.y_ + ua*dy1;
94 return NOT_INTERSECTING;
100 LineSegment linesegment0(p0, p1);
101 LineSegment linesegment1(p2, p3);
105 std::cout <<
"Line Segment 0: (" << p0.x_ <<
", " << p0.y_ <<
") to (" << p1.x_ <<
", " << p1.y_ <<
")\n" 106 <<
"Line Segment 1: (" << p2.x_ <<
", " << p2.y_ <<
") to (" << p3.x_ <<
", " << p3.y_ <<
")\n";
108 switch(linesegment0.Intersect(linesegment1, intersection))
110 case LineSegment::PARALLEL:
111 std::cout <<
"The lines are parallel\n\n";
113 case LineSegment::COINCIDENT:
114 std::cout <<
"The lines are coincident\n\n";
116 case LineSegment::NOT_INTERSECTING:
117 std::cout <<
"The lines do not intersect\n\n";
119 case LineSegment::INTERSECTING:
120 std::cout <<
"The lines intersect at (" << intersection.x_ <<
", " << intersection.y_ <<
")\n\n";
138 #endif // VPSC_LINESEGMENT_H Point Vector
A vector, represented by the Point class.
Definition: geomtypes.h:128
Definition: linesegment.h:34