Tal_Rofe

circlepoints

Jan 21st, 2018
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.10 KB | None | 0 0
  1. function [x, y] = circlepoints(r)
  2. %CIRCLEPOINTS  Returns integer points close to a circle
  3. %   [X, Y] = CIRCLEPOINTS(R) where R is a scalar returns coordinates of
  4. %   integer points close to a circle of radius R, such that none is
  5. %   repeated and there are no gaps in the circle (under 8-connectivity).
  6. %
  7. %   If R is a row vector, a circle is generated for each element of R and
  8. %   the points concatenated.
  9.  
  10. %   Copyright David Young 2010
  11.  
  12. x = [];
  13. y = [];
  14. for rad = r
  15.     [xp, yp] = circlepoints1(rad);
  16.     x = [x xp];
  17.     y = [y yp];
  18. end
  19.  
  20. end
  21.    
  22. function [x, y] = circlepoints1(r)    
  23. % Get number of rows needed to cover 1/8 of the circle
  24. l = round(r/sqrt(2));
  25. if round(sqrt(r.^2 - l.^2)) < l   % if crosses diagonal
  26.     l = l-1;
  27. end
  28. % generate coords for 1/8 of the circle, a dot on each row
  29. x0 = 0:l;
  30. y0 = round(sqrt(r.^2 - x0.^2));
  31. % Check for overlap
  32. if y0(end) == l
  33.     l2 = l;
  34. else
  35.     l2 = l+1;
  36. end
  37. % assemble first quadrant
  38. x = [x0 y0(l2:-1:2)];
  39. y = [y0 x0(l2:-1:2)];
  40. % add next quadrant
  41. x0 = [x y];
  42. y0 = [y -x];
  43. % assemble full circle
  44. x = [x0 -x0];
  45. y = [y0 -y0];
  46. end
Add Comment
Please, Sign In to add comment