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

edge detectioninpolygonnetwork problemshaperead

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!

Best 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.