Circle Drawing Algorithm in Matlab  

Posted by shashank in


Reading values located on a circle form a part of the template matching algorithm I am planning to implement. Hence I have written the following Matlab code.

Original source for the algorithm: http://cs.unc.edu/~mcmillan/comp136/Lecture7/circle.html

function imgmat = drwcir(imgmat, xCenter, yCenter, radius)

x = 0;

y = radius;

p = floor((5 - radius*4)/4);

imgmat = cirpoints(imgmat, xCenter, yCenter, 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

imgmat = cirpoints(imgmat, xCenter, yCenter, x, y);

end

return;

function img = cirpoints(img, cx, cy, x, y)

if (x == 0)

img = setpix(img, cx, cy + y);

img = setpix(img, cx, cy - y);

img = setpix(img, cx + y, cy);

img = setpix(img, cx - y, cy);

elseif (x == y)

img = setpix(img, cx + x, cy + y);

img = setpix(img, cx - x, cy + y);

img = setpix(img, cx + x, cy - y);

img = setpix(img, cx - x, cy - y);

elseif (x < y)

img = setpix(img, cx + x, cy + y);

img = setpix(img, cx - x, cy + y);

img = setpix(img, cx + x, cy - y);

img = setpix(img, cx - x, cy - y);

img = setpix(img, cx + y, cy + x);

img = setpix(img, cx - y, cy + x);

img = setpix(img, cx + y, cy - x);

img = setpix(img, cx - y, cy - x);

end

return;

function img = setpix(img,x,y)

img(x,y)=1;

return;



The following commands result in the accompanying picture:


figure;
image=zeros(500);
for i=1:2:250,
image=drwcir(image,250,250,i);
end
imshow(image);


This entry was posted on Wednesday, November 4, 2009 at 8:18 PM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment