How can I fill multiple polygons from the same vector?
9 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
jonas
on 10 Aug 2018
Edited: jonas
on 21 Aug 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 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
