# Polyshape function intersect returns incorrect polygon shape

11 views (last 30 days)
Alexander Kobyzhev on 9 Mar 2022
I'm looking for the intersection of two polygons, where one (convertedPlaneObjectPolyshape/area.mat) is the camera view area, and the second (anotherConvertedPlaneObjectPolyshape/mask.mat) is the room mask that the view area needs to be clipped to. View area is blue and the room mask is red: After using the intersect() function, I expect to get a polygon like this (the line above the hole must be present, since there is a very small distance between the area polygon and the mask): But at the output I get this polyshape: The figure shows that for some reason the hole is part of the polyshape, while the area of ​​the polyshape is calculated correctly through the area() function, that is, the hole is not part of the shape itself.
By calling the holes() and regions() functions, I get the correct data (the hole is red, the region is green), however, by calling the triangulation() function, the hole for some reason triangulates, although it should not (blue outline). For a long time working with polyshapes, I always noticed that if a polygon has a hole, then first in the array of polyshape vertices there are points of the region, then NaN, after which there are points of the hole. However, in my case, at the output, the polygon has the reverse order of the vertices in the array, that is, the hole comes first, then NaN, and at the end the region. This may not affect performance in any way, just my comment.
While looking for a solution to this problem, I accidentally noticed that by calling the regions() function, I get the same polyshape, only the vertex array is sorted correctly, as I described earlier. When it is displayed, the polyshape suddenly becomes correct, as does its triangulation: I think this is a bug in the intersect() function, because in my search for a solution to the problem, I came across similar questions where it turned out that the bug was due to matlab itself. I don't have any more guesses. If you have an explanation for this or even a solution to the problem, I will be very grateful. I am attaching all the initial data, as well as a script that demonstrates the whole essence of the problem. The script is written in R2021b latest version.

### Categories

Find more on Elementary Polygons in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!