How to check if two given line segments intersect? | GeeksforGeeks
Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.
Also check http://massivealgorithms.blogspot.com/2014/09/Chapter33-computational-geometry.html
http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf
How to Compute the Orientation
• slope of segment (p1,p2): σ = (y2−y1) / (x2−x1)
• slope of segment (p2,p3): τ = (y3−y2) / (x3−x2)
• Orientation test
- counterclockwise (left turn): σ < τ
- clockwise (right turn): σ > τ
- collinear (left turn): σ = τ
• The orientation depends on whether the expression (y2−y1) (x3−x2) − (y3−y2) (x2−x1) is positive, negative, or null.
Point Inclusion
• given a polygon and a point, is the point inside or outside the polygon?
• orientation helps solving this problem in linear time
• Draw a horizontal line to the right of each point and extend it to infinity
• Count the number of times a line intersects the polygon.
We have: - even number ⇒ point is outside
- odd number ⇒ point is inside
Special case: intersect with special points
Simple Closed Path — Part I
• Problem: Given a set of points ...
• “Connect the dots” without crossings
• Pick the bottommost point a as the anchor point
• For each point p, compute the angle q(p) of the segment (a,p) with respect to the x-axis:
• Traversing the points by increasing angle yields a simple closed path:
- Observation:, we don’t care about the actual values of the angles. We just want to sort by angle. - Idea: use the orientation to compare angles without actually computing them!!
the orientationcan be used to compare angles without actually computing them ... Cool!
θ(p) < θ(q) ⇔ orientation(a,p,q) = CCW • We can sort the points by angle by using any “sorting-by-comparison” algorithm (e.g., heapsort or merge-sort) and replacing angle comparisons with orientation tests • We obtain an O(N log N)-time algorithm for the simple closed path problem on N points
Point intersection(Point startl, Point endl, Point start2, Point end2) {
/* Rearranging these so that, in order of x values: start is before end and
* point 1 is before point 2. This will make some of the later logic simpler. */
if (startl.x > endl.x) swap(startl, endl);
if (start2.x > end2.x) swap(start2, end2);
if (startl.x > start2.x) {
swap(startl, start2);
swap(endl, end2);
}
/* Compute lines (including slope and y-intercept). */
Line linel new Line(startl, endl);
Line line2 = new Line(start2, end2);
/* If the lines are parallel, they intercept only if they have the same y
* intercept and start 2 is on line 1. */
if (linel.slope == line2.slope) {
if (linel.yintercept == line2.yintercept &&
isBetween(startl, start2, endl)) {
return start2;
}
return null;
}
/* Get intersection coordinate. */
double x = (line2.yintercept - linel.yintercept) / (linel.slope - line2.slope);
double y = x * linel.slope + linel.yintercept;
Point intersection = new Point(x, y);
/* Check if within line segment range. */
if (isBetween(startl, intersection, endl) &&
isBetween(start2, intersection, end2)) {
return intersection;
}
return null;
}
/* Checks if middle is between start and end. */
boolean isBetween(double start, double middle, double end) {
if (start > end) {
return end<= middle && middle<= start;
} else {
return start<= middle && middle<= end;
}
}
/* Checks if middle is between start and end. */
boolean isBetween(Point start, Point middle, Point end) {
return isBetween(start.x, middle.x, end.x) &&
isBetween(start.y, middle.y, end.y);
}
/* Swap coordinates of point one and two. */
void swap(Point one, Point two) {
double x one.x;
double y = one.y;
one.setlocation(two.x, two.y);
two.setlocation(x, y);
}
public class Line {
public double slope, yintercept;
public Line(Point start, Point end) {
double deltaY = end.y - start.y;
double deltaX = end.x - start.x;
slope = deltaY/deltaX; // Will be Infinity (not exception) when deltaX 0
yintercept = end.y - slope * end.x;
}
}
public class Point {
public double x, y;
public Point(double x, double y) {
this.x x;
this.y = y;
}
public void setlocation(double x, double y) {
this.x x;
this.y = y;
}
}
http://www.geeksforgeeks.org/convex-hull-set-2-graham-scan/
Also check http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
Read full article from How to check if two given line segments intersect? | GeeksforGeeks
Given two line segments (p1, q1) and (p2, q2), find if the given line segments intersect with each other.
Orientation of an ordered triplet of points in the plane can be
–counterclockwise
–clockwise
–colinear
The following diagram shows different possible orientations of (a, b, c)
–counterclockwise
–clockwise
–colinear
The following diagram shows different possible orientations of (a, b, c)
1. General Case:
- (p1, q1, p2) and (p1, q1, q2) have different orientations and
- (p2, q2, p1) and (p2, q2, q1) have different orientations
- (p1, q1, p2) and (p1, q1, q2) have different orientations and
- (p2, q2, p1) and (p2, q2, q1) have different orientations
2. Special Case
- (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are all collinear and
- the x-projections of (p1, q1) and (p2, q2) intersect
- the y-projections of (p1, q1) and (p2, q2) intersect
- (p1, q1, p2), (p1, q1, q2), (p2, q2, p1), and (p2, q2, q1) are all collinear and
- the x-projections of (p1, q1) and (p2, q2) intersect
- the y-projections of (p1, q1) and (p2, q2) intersect
bool
onSegment(Point p, Point q, Point r)
{
if
(q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return
true
;
return
false
;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int
orientation(Point p, Point q, Point r)
{
// See 10th slides from following link for derivation of the formula
// http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf
int
val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if
(val == 0)
return
0;
// colinear
return
(val > 0)? 1: 2;
// clock or counterclock wise
}
// The main function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool
doIntersect(Point p1, Point q1, Point p2, Point q2)
{
// Find the four orientations needed for general and
// special cases
int
o1 = orientation(p1, q1, p2);
int
o2 = orientation(p1, q1, q2);
int
o3 = orientation(p2, q2, p1);
int
o4 = orientation(p2, q2, q1);
// 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(p1, p2, q1))
return
true
;
// p1, q1 and p2 are colinear and q2 lies on segment p1q1
if
(o2 == 0 && onSegment(p1, q2, q1))
return
true
;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if
(o3 == 0 && onSegment(p2, p1, q2))
return
true
;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if
(o4 == 0 && onSegment(p2, q1, q2))
return
true
;
return
false
;
// Doesn't fall in any of the above cases
}
http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf
How to Compute the Orientation
• slope of segment (p1,p2): σ = (y2−y1) / (x2−x1)
• slope of segment (p2,p3): τ = (y3−y2) / (x3−x2)
• Orientation test
- counterclockwise (left turn): σ < τ
- clockwise (right turn): σ > τ
- collinear (left turn): σ = τ
• The orientation depends on whether the expression (y2−y1) (x3−x2) − (y3−y2) (x2−x1) is positive, negative, or null.
Point Inclusion
• given a polygon and a point, is the point inside or outside the polygon?
• orientation helps solving this problem in linear time
• Draw a horizontal line to the right of each point and extend it to infinity
• Count the number of times a line intersects the polygon.
We have: - even number ⇒ point is outside
- odd number ⇒ point is inside
Special case: intersect with special points
Simple Closed Path — Part I
• Problem: Given a set of points ...
• “Connect the dots” without crossings
• Pick the bottommost point a as the anchor point
• For each point p, compute the angle q(p) of the segment (a,p) with respect to the x-axis:
• Traversing the points by increasing angle yields a simple closed path:
- Observation:, we don’t care about the actual values of the angles. We just want to sort by angle. - Idea: use the orientation to compare angles without actually computing them!!
the orientationcan be used to compare angles without actually computing them ... Cool!
θ(p) < θ(q) ⇔ orientation(a,p,q) = CCW • We can sort the points by angle by using any “sorting-by-comparison” algorithm (e.g., heapsort or merge-sort) and replacing angle comparisons with orientation tests • We obtain an O(N log N)-time algorithm for the simple closed path problem on N points
Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any
/* Rearranging these so that, in order of x values: start is before end and
* point 1 is before point 2. This will make some of the later logic simpler. */
if (startl.x > endl.x) swap(startl, endl);
if (start2.x > end2.x) swap(start2, end2);
if (startl.x > start2.x) {
swap(startl, start2);
swap(endl, end2);
}
/* Compute lines (including slope and y-intercept). */
Line linel new Line(startl, endl);
Line line2 = new Line(start2, end2);
/* If the lines are parallel, they intercept only if they have the same y
* intercept and start 2 is on line 1. */
if (linel.slope == line2.slope) {
if (linel.yintercept == line2.yintercept &&
isBetween(startl, start2, endl)) {
return start2;
}
return null;
}
/* Get intersection coordinate. */
double x = (line2.yintercept - linel.yintercept) / (linel.slope - line2.slope);
double y = x * linel.slope + linel.yintercept;
Point intersection = new Point(x, y);
/* Check if within line segment range. */
if (isBetween(startl, intersection, endl) &&
isBetween(start2, intersection, end2)) {
return intersection;
}
return null;
}
/* Checks if middle is between start and end. */
boolean isBetween(double start, double middle, double end) {
if (start > end) {
return end<= middle && middle<= start;
} else {
return start<= middle && middle<= end;
}
}
/* Checks if middle is between start and end. */
boolean isBetween(Point start, Point middle, Point end) {
return isBetween(start.x, middle.x, end.x) &&
isBetween(start.y, middle.y, end.y);
}
/* Swap coordinates of point one and two. */
void swap(Point one, Point two) {
double x one.x;
double y = one.y;
one.setlocation(two.x, two.y);
two.setlocation(x, y);
}
public class Line {
public double slope, yintercept;
public Line(Point start, Point end) {
double deltaY = end.y - start.y;
double deltaX = end.x - start.x;
slope = deltaY/deltaX; // Will be Infinity (not exception) when deltaX 0
yintercept = end.y - slope * end.x;
}
}
public class Point {
public double x, y;
public Point(double x, double y) {
this.x x;
this.y = y;
}
public void setlocation(double x, double y) {
this.x x;
this.y = y;
}
}
http://www.geeksforgeeks.org/convex-hull-set-2-graham-scan/
Also check http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
Read full article from How to check if two given line segments intersect? | GeeksforGeeks