Is there a function to identify whether polylines are located inside a polygonal region?
2 views (last 30 days)
Show older comments
Taufiq Rashid
on 2 May 2018
Commented: 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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190270/image.jpeg)
2 Comments
Walter Roberson
on 2 May 2018
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?
Accepted Answer
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.
2 Comments
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.
More Answers (0)
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!