Beispiel #1
0
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool segmentsIntersect(Line2 l1, Line2 l2)
{
	// Find the four orientations needed for general and
	// special cases
	int o1 = orientation(l1.getStart(), l1.getEnd(), l2.getStart());
	int o2 = orientation(l1.getStart(), l1.getEnd(), l2.getEnd());
	int o3 = orientation(l2.getStart(), l2.getEnd(), l1.getStart());
	int o4 = orientation(l2.getStart(), l2.getEnd(), l1.getEnd());

	// General case
	if (o1 != o2 && o3 != o4)
		return true;

	// Special Cases
	// p1, q1 and p2 are colinear and p2 lies on segment p1q1
	if (o1 == 0 && onSegment(l1.getStart(), l2.getStart(), l1.getEnd())) return true;

	// p1, q1 and p2 are colinear and q2 lies on segment p1q1
	if (o2 == 0 && onSegment(l1.getStart(), l2.getEnd(), l1.getEnd())) return true;

	// p2, q2 and p1 are colinear and p1 lies on segment p2q2
	if (o3 == 0 && onSegment(l2.getStart(), l1.getStart(), l2.getEnd())) return true;

	// p2, q2 and q1 are colinear and q1 lies on segment p2q2
	if (o4 == 0 && onSegment(l2.getStart(), l1.getEnd(), l2.getEnd())) return true;

	return false; // Doesn't fall in any of the above cases
}
Beispiel #2
0
bool oppositeSide(Point2 p1, Point2 p2, Line2 ab)//returns whether or not p1 and p2 are on the same side of AB
{
	double & ax = p1.x;
	double & ay = p1.y;

	double & bx = p2.x;
	double & by = p2.y;

	double & x1 = ab.x;
	double & y1 = ab.y;

	double x2 = ab.getEnd().x;
	double y2 = ab.getEnd().y;

	return ((y1 - y2)*(ax - x1)+(x2 - x1)*(ay - y1))*((y1 - y2)*(bx - x1)+(x2 - x1)*(by - y1))<0;
}
Beispiel #3
0
bool operator==(Line2 const & lhs, Line2 const & rhs)
{
	return (lhs.getStart() == rhs.getStart() && lhs.getEnd() == rhs.getEnd()) || (lhs.getStart() == rhs.getEnd() && lhs.getEnd() == rhs.getStart());
}
Beispiel #4
0
bool intersect(Line2 ab, Line2 cd)
{
	return oppositeSide(ab, ab.getEnd(), cd) && oppositeSide(cd, cd.getEnd(), ab);
}