Why is it that if I try to fill a strange polygon it comes out of the edges?

1 view (last 30 days)
Hi, I have a somewhat strange polygon with overlaps and in this case I am experiencing problems, which in all other cases I don't, in filling it. Could you guys help me solve it?
x0 = [45;45;50;50;50;50;50;55;55;55;55;55;55;55;55;55;55;55;55;55;55;55;55;50;50;50;50;50;45;45];
y0 = [1.6;2.2;1.9;1.9;2.1;2.2;2.2;1.6;1.6;1.6;1.6;1.6;1.6;1.9;2.1;2.3;2.1;2;2;2;2;2;2;2.4;2.4;2.3;2.1;2.1;2.4;2];
plot(x0,yo)
fill(x0,y0,'r')
  3 Comments
Luca Terzo
Luca Terzo on 22 Nov 2022
In the sense that I see an outer part of the polygon being filled, while I would like only the inner part of the closed lines to be filled. I cannot modify the polygon at will since it is obtained by calculating certain metrics
Luca Terzo
Luca Terzo on 22 Nov 2022
I have other case scenarios of polygons in overlaps where the problem does not arise like this one:

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 22 Nov 2022
Using polyshape is one option, and likely the only approach that would work here —
x0 = [45;45;50;50;50;50;50;55;55;55;55;55;55;55;55;55;55;55;55;55;55;55;55;50;50;50;50;50;45;45];
y0 = [1.6;2.2;1.9;1.9;2.1;2.2;2.2;1.6;1.6;1.6;1.6;1.6;1.6;1.9;2.1;2.3;2.1;2;2;2;2;2;2;2.4;2.4;2.3;2.1;2.1;2.4;2];
ps = polyshape(x0,y0);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
figure
plot(x0,y0)
hold on
plot(ps, 'FaceColor','r')
hold off
Experiment to get different results.
.

More Answers (0)

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!