Polyshape/union returns incorrect polygon shape

I am combining multiple overlapping small rectangles (randomly placed and oriented) together in a larger square area (one by one whitin a loop) and computing the resulting polygon shape. I use both the intersect and union commands. For some reason, ocasionally during this process the union command between the square and one rectangle results in an incorrect shape. I assume that this is related with some numerical error that occurs when the vertices of the rectangle and the square are nearby. This is the square before the rectangle is added
and this is the union shape when the rectangle is added
notice how when the rectangle is added to the square the polygon becomes distorted (for some reason) almost as if the command wants to join the vertices of both. Finally, I then ask for the intersection between this shape and the square, resulting in a smaller area than what I had originally
For reference, I am also adding a figure with the generated rectangles in red and the offensive rectangle is shown in blue
Thank you in advance for any help. I have tested and this problem occurs both in Matlab 2018a and Matlab 2019a.

7 Comments

It might help if you post the code that generated these images.
Indeed, I have a attached a minimal working example .m and .mat files. Please let me know if anything else is necessary
I'm not seeing what you see (see figure below). I notice there are some NaN values in the poly_before vertices. Perhaps that's an issue.
Can you compare that against the poly_during that is also included in the mat file? Also, what version of Matlab are you using? Thanks.
The NaN is probably not the problem since that is how Matlab defines holes. For example, in this tutorial of a square with a hole the polyshape P has the following vertices
P =
0 0
0 2.0000
2.0000 2.0000
2.0000 0
NaN NaN
0.5000 0.5000
0.5000 1.5000
1.5000 1.5000
1.5000 0.5000
NaN NaN
3.0000 0.5000
3.5000 1.5000
4.0000 0.5000
NaN NaN
3.2500 0.6000
3.7500 0.6000
3.5000 1.0000
Not sure what you mean by "that" in Can you compare that against the poly_during. I'm running R2021a
I meant the poly_new created by the script. I will try to run the MWE on Matlab R2021a, as it seems to be a version dependent problem.

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 10 Jul 2021
Edited: Matt J on 10 Jul 2021
Like Scott, I find the result doesn't reproduce (R2020b). I strongly suspect what you're seeing might be the same as this acknowledged polyshape.union() bug bug in earlier Matlab versions.
Regardless of whether it's the same bug, if you're making heavy use of polyshape, it would obviously be best to upgrade to a version without bugs, if you can.

1 Comment

Indeed, it is probably the same bug. It would seem that 2019a still has this issue, so upgrade to 2020 or 2021 is the best option. Thanks!

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

on 10 Jul 2021

Edited:

on 10 Jul 2021

Community Treasure Hunt

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

Start Hunting!