Code covered by the BSD License  

Highlights from
Three integer algorithms drawing circles

image thumbnail

Three integer algorithms drawing circles

by

 

Midpoint and two versions of Bresenham algorithm for drawing circle.

[X,Y]=midpoint_circle(r)
% TITLE     :MIDPOINT CIRCLE ALGORITHM
% SUBJECT   :COMPUTER GRAPHICS AND SOLID MODELLING
% http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture6/circle.html
function [X,Y]=midpoint_circle(r)
% function [X,Y]=midpoint_circle2(r)
% return coordinates of circle points by midpoint algo
% center is (0,0)

x = 0;
y = r;
p = (5 - r*4)/4;

[X,Y]=circlePoints(x, y);
while x < y
    x=x+1;
    if (p < 0)
        p =p+ 2*x+1;
    else
        y=y-1;
        p =p+ 2*(x-y)+1;
    end
    [xx,yy]=circlePoints(x, y);
    X=[X,xx];
    Y=[Y,yy];
end

function [xx,yy]=circlePoints(x,y)
if (x == 0)
    xx=[0 0 y -y];
    yy=[y -y 0 0];
    return;
elseif x == y
    xx=[x -x x -x];
    yy=[y y -y -y];
    return;
elseif x < y
    xx=[x -x x -x y -y y -y];
    yy=[y y -y -y x x -x -x];
    return;
else
    xx=[];
    yy=[];
end

Contact us