MATLAB Answers

0

Is there a function to identify whether polylines are located inside a polygonal region?

Asked by Taufiq Rashid on 2 May 2018
Latest activity Commented on by Walter Roberson
on 23 May 2018
I am trying to create a network which will first generate some random points within a polygon and then connect those points using polylines. The polygon has few holes in it and I don't want my network to pass through those holes. The function "inpolygon" is able to identify whether or not a point is inside a polygon. So, none of my points are generated within those holes. However, the problem is with the lines which connect those points. The lines are passing through those holes. Is there a function like 'inpolygon' which will be able to identify whether a line is inside a polygon? I have attached a screenshot of my problem. Any kind of help will be highly appreciated. Thanks!

  2 Comments

Do the holes follow the convention of having their vertices in the other order (clockwise / counterclockwise) from your polygon exterior?
Also, do you have R2017b or later?
I think they don't because I imported the polygon from ArcMap. I am not sure whether ArcMap follows the convention of assigning vertices in reverse order.
I am currently using R2016b but I might be able to manage R2017b if needed.

Sign in to comment.

1 Answer

Answer by Kelly Kearney
on 3 May 2018
Edited by Kelly Kearney
on 3 May 2018
 Accepted Answer

You can try out my lineinpolygon.m function. (Sorry, the GitHub repo doesn't have a proper readme yet... you'll need to copy the lineinpolygon.m and inpolygons.m functions to your path).
The function finds the intersection points of the line and the polygon, then determines which resulting line segments are inside and outside the polygon. It does assume the clockwise=external and counterclockwise=internal contour (i.e. hole); most shapefiles follow this convention.
Right now this function uses the Mapping Toolbox polygon functions. I plan to update it to use the newer polyshape objects that were introduced in recent versions, but I haven't found time for that yet.

  2 Comments

Ah, but where should they be connected?
If you use the sequence "generate a batch of points, connect the ones in sequence that would not cross any of the holes, leave unconnected any edge that would cross a hole", then you are pretty likely to encounter this scenario.
If your lines must be connected in sequence, then you are going to have to generate them in such a way that each new candidate point does not cross a hole relative to the point it is being generated off of. If you have minimum distance constraints or if you have constraints about not crossing an edge that you created earlier in the sequence, then you are likely to end up stuck in "dead ends" if you enter any peninsula.

Sign in to comment.