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

3 views (last 30 days)
Luca Terzo on 22 Nov 2022
Commented: Star Strider on 22 Nov 2022
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')
Luca Terzo on 22 Nov 2022
I have other case scenarios of polygons in overlaps where the problem does not arise like this one:

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.
.
##### 2 CommentsShowHide 1 older comment
Star Strider on 22 Nov 2022
As always, my pleasure!

### Categories

Find more on Elementary Polygons in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by