-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirclepoints.m
46 lines (42 loc) · 1.06 KB
/
circlepoints.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [x, y] = circlepoints(r)
%CIRCLEPOINTS Returns integer points close to a circle
% [X, Y] = CIRCLEPOINTS(R) where R is a scalar returns coordinates of
% integer points close to a circle of radius R, such that none is
% repeated and there are no gaps in the circle (under 8-connectivity).
%
% If R is a row vector, a circle is generated for each element of R and
% the points concatenated.
% Copyright David Young 2010
x = [];
y = [];
for rad = r
[xp, yp] = circlepoints1(rad);
x = [x xp];
y = [y yp];
end
end
function [x, y] = circlepoints1(r)
% Get number of rows needed to cover 1/8 of the circle
l = round(r/sqrt(2));
if round(sqrt(r.^2 - l.^2)) < l % if crosses diagonal
l = l-1;
end
% generate coords for 1/8 of the circle, a dot on each row
x0 = 0:l;
y0 = round(sqrt(r.^2 - x0.^2));
% Check for overlap
if y0(end) == l
l2 = l;
else
l2 = l+1;
end
% assemble first quadrant
x = [x0 y0(l2:-1:2)];
y = [y0 x0(l2:-1:2)];
% add next quadrant
x0 = [x y];
y0 = [y -x];
% assemble full circle
x = [x0 -x0];
y = [y0 -y0];
end