Java中Polygon与Line相交判断异常问题求助
Hey there! Let's break down why those Java 2D methods aren't working for your polygon-infinite line intersection checks, and fix it up.
Why Your Current Approaches Fail
First, let's clarify why Shape.intersectLine(Line2D) and Area.intersects(Area) aren't cutting it:
Shape.intersectLine(Line2D): This method only checks if the provided line segment (not an infinite straight line) intersects the shape. If your "line" is actually an infinite line but you're passing a segment that doesn't touch the polygon's edges, it'll return false even if the infinite line cuts through the polygon's interior.Area.intersects(Area): Areas represent closed, 2D regions with area. A straight line has no area, so converting it to an Area creates a zero-area region. This method will only return true if the line perfectly overlaps an edge of the polygon—completely missing cases where the line cuts through the polygon's interior.
A Working Solution for Infinite Line + Polygon Intersection
To properly check if an infinite line intersects a polygon, we need to:
- Check if the line intersects any of the polygon's edges (line segments).
- Check if any polygon vertex lies directly on the line.
Here's a concrete implementation:
import java.awt.Polygon; import java.awt.geom.Line2D; public class PolygonLineIntersection { // Check if an infinite line intersects a polygon public static boolean lineIntersectsPolygon(Line2D infiniteLine, Polygon polygon) { int vertexCount = polygon.npoints; // Check intersection with each polygon edge for (int i = 0; i < vertexCount; i++) { int x1 = polygon.xpoints[i]; int y1 = polygon.ypoints[i]; int x2 = polygon.xpoints[(i + 1) % vertexCount]; int y2 = polygon.ypoints[(i + 1) % vertexCount]; Line2D edge = new Line2D.Double(x1, y1, x2, y2); if (doesInfiniteLineIntersectSegment(infiniteLine, edge)) { return true; } } // Check if any vertex lies on the infinite line for (int i = 0; i < vertexCount; i++) { if (infiniteLine.ptSegDist(polygon.xpoints[i], polygon.ypoints[i]) == 0) { return true; } } return false; } // Helper: Check if infinite line intersects a line segment private static boolean doesInfiniteLineIntersectSegment(Line2D line, Line2D segment) { // Use relativeCCW to check if segment endpoints are on opposite sides of the line double ccw1 = line.relativeCCW(segment.getX1(), segment.getY1()); double ccw2 = line.relativeCCW(segment.getX2(), segment.getY2()); // If endpoints are on opposite sides, or one is on the line, they intersect return (ccw1 * ccw2) <= 0; } // Test example public static void main(String[] args) { Polygon square = new Polygon(new int[]{0, 0, 100, 100}, new int[]{0, 100, 100, 0}, 4); // Infinite line y=50 (represented with a long segment, but our method treats it as infinite) Line2D horizontalLine = new Line2D.Double(-50, 50, 150, 50); System.out.println(lineIntersectsPolygon(horizontalLine, square)); // Output: true } }
Notes for Your Use Case
- If you actually need to check for line segments (not infinite lines) intersecting the polygon, you can use
Shape.intersectsLine()—just make sure yourLine2Drepresents the segment you want to test. - The
Polygonclass automatically closes the shape by connecting the last vertex back to the first, so our edge loop with(i+1)%vertexCountworks correctly even for non-closed input arrays.
内容的提问来源于stack exchange,提问作者cdubbs
相关产品推荐
相关产品推荐

