Code covered by the BSD License

# 3 integer algorithms for drawing circles

by

### Xenya (view profile)

3 implementations of Bresenham and Midpoint algorithm.

circle_test.m
```[X1,Y1]=midpoint_circle(7);
[X2,Y2]=bresenham_circle(7,3);
[X3,Y3]=bresenham_circle2(7);
figure,subplot(1,3,1),plot(X1,Y1,'g.'),axis equal,title('Midpoint')
subplot(1,3,2),plot(X2,Y2,'r.'),axis equal,title('Bresenham-1')
subplot(1,3,3),plot(X3,Y3,'b.'),axis equal,title('Bresenham-2')

% image example 1
% draw single circle
I=zeros(200);
X_center=70; Y_center=150;
I(sub2ind(size(I),X1+X_center,Y1+Y_center))=1; figure,imshow(I);

% image example 2
% draw a bunch of tiny circles
Sz=500; I=zeros(Sz);
N=25; % # of circles
Cx=round(rand([N 1])*Sz);
Cy=round(rand([N 1])*Sz);

xx = bsxfun(@plus,repmat(X1,[N 1]),Cx);
yy = bsxfun(@plus,repmat(Y1,[N 1]),Cy);

% remove points out of range
ii_bad=find(xx<1 | xx>Sz | yy<1 | yy>Sz);
xx(ii_bad)=[]; yy(ii_bad)=[];
I(sub2ind(size(I),xx,yy))=1; figure,imshow(I);

```

Contact us