% --- Executes on button press in ellipse.
function ellipse_Callback(hObject, eventdata, handles)
global count shape mx my globcount
%'count' is a global variable that keeps track of current number of
%shapes
[nx,ny]=ginput(2);
x1=round(nx(1));
y1=round(ny(1));
x2=round(nx(2));
y2=round(ny(2));
if (x1>0)&&(y1>0)&&(x2>0)&&(y2>0)&&(x1<my)&&(y1<mx)&&(x2<my)&&(y2<mx)
    %executes only if points are within the the shown axis limits
    x=round(min(nx));
    y=round(min(ny));
    w=x1-x2;          %w=width
    h=y1-y2;          %h=height
    w=abs(w);
    h=abs(h);
    if (w~=0)&&(h~=0)
        count=count+1;
        globcount=globcount+1;
        rectangle('Position',[x,y,w,h],'Curvature',[1 1],'FaceColor','k')
        xc=(x1+x2)/2;
        yc=(y1+y2)/2;
        a=w/2;
        b=h/2;
        k=1;
        for i=x:(x+w)
            leaf=(sqrt(1-(((i-xc)/a)^2)))*b;
            yup=round(yc+leaf);
            ydown=round(yc-leaf); 
            for j=ydown:yup
                elem(k,1)=i;
                elem(k,2)=j;
                k=k+1;
            end               
        end
        %'elem' is formed at the end of this loop
        shape(count).elem=elem;
        shape(count).id=0;
        shape(count).seq=globcount;
        limca(1,1)=x;
        limca(1,2)=y;
        limca(1,3)=w;
        limca(1,4)=h;
        shape(count).position=limca;
        clear elem
    else
        msgbox('Error: Points are too close. Cant be drawn')
    end
else
    msgbox('Error: You have chosen a point outside the shown axes')
end

     