|
| 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 | +} |
0 commit comments