Skip to content

Commit 79bc620

Browse files
authored
feat(geometry): add line segment intersection utility (#7376)
* feat(geometry): add line segment intersection utility * test(geometry): cover more line intersection edge cases * Address line intersection edge cases from review * Apply clang-format fixes for line intersection
1 parent 1edf319 commit 79bc620

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.geom.Point2D;
4+
import java.util.Optional;
5+
6+
/**
7+
* Utility methods for checking and computing 2D line segment intersections.
8+
*/
9+
public final class LineIntersection {
10+
private LineIntersection() {
11+
}
12+
13+
/**
14+
* Checks whether two line segments intersect.
15+
*
16+
* @param p1 first endpoint of segment 1
17+
* @param p2 second endpoint of segment 1
18+
* @param q1 first endpoint of segment 2
19+
* @param q2 second endpoint of segment 2
20+
* @return true when the segments intersect (including touching endpoints)
21+
*/
22+
public static boolean intersects(Point p1, Point p2, Point q1, Point q2) {
23+
int o1 = orientation(p1, p2, q1);
24+
int o2 = orientation(p1, p2, q2);
25+
int o3 = orientation(q1, q2, p1);
26+
int o4 = orientation(q1, q2, p2);
27+
28+
if (o1 != o2 && o3 != o4) {
29+
return true;
30+
}
31+
32+
if (o1 == 0 && onSegment(p1, q1, p2)) {
33+
return true;
34+
}
35+
if (o2 == 0 && onSegment(p1, q2, p2)) {
36+
return true;
37+
}
38+
if (o3 == 0 && onSegment(q1, p1, q2)) {
39+
return true;
40+
}
41+
if (o4 == 0 && onSegment(q1, p2, q2)) {
42+
return true;
43+
}
44+
45+
return false;
46+
}
47+
48+
/**
49+
* Computes the single geometric intersection point between two non-parallel
50+
* segments when it exists.
51+
*
52+
* <p>For parallel/collinear overlap, this method returns {@code Optional.empty()}.
53+
*
54+
* @param p1 first endpoint of segment 1
55+
* @param p2 second endpoint of segment 1
56+
* @param q1 first endpoint of segment 2
57+
* @param q2 second endpoint of segment 2
58+
* @return the intersection point when uniquely defined and on both segments
59+
*/
60+
public static Optional<Point2D.Double> intersectionPoint(Point p1, Point p2, Point q1, Point q2) {
61+
if (!intersects(p1, p2, q1, q2)) {
62+
return Optional.empty();
63+
}
64+
65+
long x1 = p1.x();
66+
long y1 = p1.y();
67+
long x2 = p2.x();
68+
long y2 = p2.y();
69+
long x3 = q1.x();
70+
long y3 = q1.y();
71+
long x4 = q2.x();
72+
long y4 = q2.y();
73+
74+
long denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
75+
if (denominator == 0L) {
76+
return sharedEndpoint(p1, p2, q1, q2);
77+
}
78+
79+
long determinant1 = x1 * y2 - y1 * x2;
80+
long determinant2 = x3 * y4 - y3 * x4;
81+
long numeratorX = determinant1 * (x3 - x4) - (x1 - x2) * determinant2;
82+
long numeratorY = determinant1 * (y3 - y4) - (y1 - y2) * determinant2;
83+
84+
return Optional.of(new Point2D.Double(numeratorX / (double) denominator, numeratorY / (double) denominator));
85+
}
86+
87+
private static int orientation(Point a, Point b, Point c) {
88+
long cross = ((long) b.x() - a.x()) * ((long) c.y() - a.y()) - ((long) b.y() - a.y()) * ((long) c.x() - a.x());
89+
return Long.compare(cross, 0L);
90+
}
91+
92+
private static Optional<Point2D.Double> sharedEndpoint(Point p1, Point p2, Point q1, Point q2) {
93+
if (p1.equals(q1) || p1.equals(q2)) {
94+
return Optional.of(new Point2D.Double(p1.x(), p1.y()));
95+
}
96+
if (p2.equals(q1) || p2.equals(q2)) {
97+
return Optional.of(new Point2D.Double(p2.x(), p2.y()));
98+
}
99+
return Optional.empty();
100+
}
101+
102+
private static boolean onSegment(Point a, Point b, Point c) {
103+
return b.x() >= Math.min(a.x(), c.x()) && b.x() <= Math.max(a.x(), c.x()) && b.y() >= Math.min(a.y(), c.y()) && b.y() <= Math.max(a.y(), c.y());
104+
}
105+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.thealgorithms.geometry;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.awt.geom.Point2D;
8+
import java.util.Optional;
9+
import org.junit.jupiter.api.Test;
10+
11+
class LineIntersectionTest {
12+
13+
@Test
14+
void testCrossingSegments() {
15+
Point p1 = new Point(0, 0);
16+
Point p2 = new Point(4, 4);
17+
Point q1 = new Point(0, 4);
18+
Point q2 = new Point(4, 0);
19+
20+
assertTrue(LineIntersection.intersects(p1, p2, q1, q2));
21+
Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2);
22+
assertTrue(intersection.isPresent());
23+
assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9);
24+
assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9);
25+
}
26+
27+
@Test
28+
void testParallelSegments() {
29+
Point p1 = new Point(0, 0);
30+
Point p2 = new Point(3, 3);
31+
Point q1 = new Point(0, 1);
32+
Point q2 = new Point(3, 4);
33+
34+
assertFalse(LineIntersection.intersects(p1, p2, q1, q2));
35+
assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty());
36+
}
37+
38+
@Test
39+
void testTouchingAtEndpoint() {
40+
Point p1 = new Point(0, 0);
41+
Point p2 = new Point(2, 2);
42+
Point q1 = new Point(2, 2);
43+
Point q2 = new Point(4, 0);
44+
45+
assertTrue(LineIntersection.intersects(p1, p2, q1, q2));
46+
Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2);
47+
assertTrue(intersection.isPresent());
48+
assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9);
49+
assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9);
50+
}
51+
52+
@Test
53+
void testCollinearOverlapHasNoUniquePoint() {
54+
Point p1 = new Point(0, 0);
55+
Point p2 = new Point(4, 4);
56+
Point q1 = new Point(2, 2);
57+
Point q2 = new Point(6, 6);
58+
59+
assertTrue(LineIntersection.intersects(p1, p2, q1, q2));
60+
assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty());
61+
}
62+
63+
@Test
64+
void testCollinearDisjointSegments() {
65+
Point p1 = new Point(0, 0);
66+
Point p2 = new Point(2, 2);
67+
Point q1 = new Point(3, 3);
68+
Point q2 = new Point(5, 5);
69+
70+
assertFalse(LineIntersection.intersects(p1, p2, q1, q2));
71+
assertTrue(LineIntersection.intersectionPoint(p1, p2, q1, q2).isEmpty());
72+
}
73+
74+
@Test
75+
void testCollinearSegmentsTouchingAtEndpointHaveUniquePoint() {
76+
Point p1 = new Point(0, 0);
77+
Point p2 = new Point(2, 2);
78+
Point q1 = new Point(2, 2);
79+
Point q2 = new Point(4, 4);
80+
81+
assertTrue(LineIntersection.intersects(p1, p2, q1, q2));
82+
Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2);
83+
assertTrue(intersection.isPresent());
84+
assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9);
85+
assertEquals(2.0, intersection.orElseThrow().getY(), 1e-9);
86+
}
87+
88+
@Test
89+
void testVerticalAndHorizontalCrossingSegments() {
90+
Point p1 = new Point(2, 0);
91+
Point p2 = new Point(2, 5);
92+
Point q1 = new Point(0, 3);
93+
Point q2 = new Point(4, 3);
94+
95+
assertTrue(LineIntersection.intersects(p1, p2, q1, q2));
96+
Optional<Point2D.Double> intersection = LineIntersection.intersectionPoint(p1, p2, q1, q2);
97+
assertTrue(intersection.isPresent());
98+
assertEquals(2.0, intersection.orElseThrow().getX(), 1e-9);
99+
assertEquals(3.0, intersection.orElseThrow().getY(), 1e-9);
100+
}
101+
}

0 commit comments

Comments
 (0)