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);