Search code examples
matlabfillellipse

The ellipse is outlined, how to fill?


In this tutorial, an ellipse will be outlined. As you can see, a red border will be drawn around. Form such result, how can we fill such surrounded border with white, and the rest of the image as black?

Thanks.


Solution

  • A little Google search with the words fill and Matlab would tell you that there is a function called fill which performs what you want(check here).

    In the example, putting it right after the call to plot gives something like the following. I put the whole code for the for-loop:

    for k = 1:length(s)
    xbar = s(k).Centroid(1);
    ybar = s(k).Centroid(2);
    
    a = s(k).MajorAxisLength/2;
    b = s(k).MinorAxisLength/2;
    
    theta = pi*s(k).Orientation/180;
    R = [ cos(theta)   sin(theta)
         -sin(theta)   cos(theta)];
    
    xy = [a*cosphi; b*sinphi];
    xy = R*xy;
    
    x = xy(1,:) + xbar;
    y = xy(2,:) + ybar;
    
    plot(x,y,'r','LineWidth',2);
    
    fill(x,y,rand(1,3)) %// Here is the important line.
    end
    

    enter image description here

    I'll let you discover how you can fill the ellipses with white instead of random colors.