Is there a class in the Java API that I can utilize to check if a line intersects a shape, given the starting and ending coordinates. I have tried playing around with Line2D objects, but the intersects() function for that class only seems to take in rectangular coordinates and Rectangle2D objects, which doesn’t suit my purposes as my shapes can have any number of sides.
Share
Like @Dan said you can check is your line intersects borders of your
Polygonbut you don’t have to check intersect with everyline created by any two points fromPolygon, just have to check intersection of your line with every pair of two point like(p1,p2), (p2,p3), ... , (p(N-1), pN), (pN, p1). Polygon isn’t created from all combination of points in array but from line from p1 to p2 connected with line from p2 to p3 and so on till line conected from pN to p1.Some example here:
After execution of this code inside
intersectyou have value is your line intersects polygon or not.If you accept situation when Polygon can contains a line and this still means that line (as a path) intersect the polygon then create java
Polygoninstance from your points (vertex) and just invoke: