MATLAB Answers

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

3 views (last 30 days)
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!
Taufiq Rashid
Taufiq Rashid on 3 May 2018
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.

Accepted Answer

Kelly Kearney
Kelly Kearney on 3 May 2018
Edited: Kelly Kearney on 3 May 2018
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.
Walter Roberson
Walter Roberson on 23 May 2018
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.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!