How to check if a LineString intersects a LineString (or Polygon)

Viewed 278

I am using the MapBox SDK. I have ran into an issue. I am trying to create a path that avoids certain areas, which are denoted as Polygons on the map. I want to check if my LineString will intersect a Polygon. I have seen there is a method outer() which gives me the perimeter of the Polygon as a LineString, however I am not sure how to proceed.

I need to find a way to give me a boolean whether or not my LineString path will intersect the perimeter, meaning I will be able to find an alternative route.

Any help is appreciated.

1 Answers

You could implement this line-line intersection algorithm in a function. At the end of the paragraph you will also find a way to check if a line intersects a segment line.

Using the outer() method and the line-line interscection function, you can check if your LineString intersects any line segment of your Polygon.

I'm thinking of such signatures, but you could do it differently :

public boolean lineIntersectsLine(LineString line1, LineString line2){
    //line-line intersection algorithm
};

public boolean lineIntersectsSegment(LineString line, LineString segment){
    //line-line intersection algorithm + check if intersection point is on segment
};

public boolean lineIntersectsPolygon(LineString line, Polygon polygon){
    //lineIntersectSegment for each segment of polygon
};

Other links that could help you : equation of a Line from 2 Points, check if two given line segments intersect

Related