How can I fill multiple polygons from the same vector?

5 visualizaciones (últimos 30 días)
hlor
hlor el 10 de Ag. de 2018
Editada: jonas el 21 de Ag. de 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!

Respuesta aceptada

jonas
jonas el 10 de Ag. de 2018
Editada: jonas el 21 de Ag. de 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 comentarios
hlor
hlor el 11 de Ag. de 2018
No. I hope this is clearer.
jonas
jonas el 11 de Ag. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by