How can I fill multiple polygons from the same vector?

8 vues (au cours des 30 derniers jours)
hlor
hlor le 10 Août 2018
Modifié(e) : jonas le 21 Août 2018
Hi everyone! I have a problem that seems to be trivial, but that I cannot solve. I have two vectors x=[...] and y=[...]. These two vectors contain the coordinates of three polygons, divided by NaN. I would like to fill these three areas without splitting the vectors (two red squares and one red triangle). Here is the code
x = [0 1 1 0 NaN 2 3 3 2 2 NaN -1 1 1 -1 -1];
y = [0 1 2 0 NaN 3 3 4 4 3 NaN 4 4 5 5 4];
figure
plot(x,y,'k'); hold on
fill(x,y,'r','LineStyle','none')
I also tried to remove NaN, without success.
x1 = [0 1 1 0 2 3 3 2 2 -1 1 1 -1 -1];
y1 = [0 1 2 0 3 3 4 4 3 4 4 5 5 4];
figure
fill(x1,y1,'r','LineStyle','none')
Any suggestions? Thank you!

Réponse acceptée

jonas
jonas le 10 Août 2018
Modifié(e) : jonas le 21 Août 2018
Use patch. Each patch is defined by the coordinates of the columns of the input matrix. As such, all patches must have the same number of coordinates. You can pad the shorter patches by repeating the last coordinate.
x = [0 1 1 0 0, 2 3 3 2 2,-1 1 1 -1 -1];
y = [0 1 2 0 0, 3 3 4 4 3, 4 4 5 5 4];
x=reshape(x,5,3)
y=reshape(y,5,3)
h=patch(x,y,[1;0;2]);
  6 commentaires
hlor
hlor le 11 Août 2018
No. I hope this is clearer.
jonas
jonas le 11 Août 2018
Oki. Then you have to either make polygons with holes (see e.g. polyshape ) or fill by white. I don't understand why you do not want to fill them by white. You can adjust the color and/or vertices after you create the object, if you need to.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by