Search code examples
matlabdrawpolygonfillpolygons

Filling a polygon with some colour


I have the following that draws a polygon:

nVal = 33;
x = 164.8 + rand(nVal,1).*(354.6-164.8);
y = 66.3 + rand(nVal,1).*(222.3-66.3);
k = convhull(x,y);
plot(x(k),y(k),'r-',x,y,'b+')

If I want to fill out the shape, I think we can use patch. But, it seems that I'm not using it correct, as I didn't get the whole shape filled.

I used the following:

 patch(x,y,'r')

Thanks.


Solution

  • Use fill:

    fill(x(k),y(k),[.75 .75 .75]) %// light gray. Or change color as desired
    

    If you want the original points to be seen, you have to plot them after fill:

    fill(x(k),y(k),[.75 .75 .75])
    hold on
    plot(x(k),y(k),'r-',x,y,'b+')
    

    enter image description here