function r = latticepointsaroundorigin(num) % r = latticepointssorted(n) retruns a list of lattice points, % in the order of distance from the origin and polar angle. % Uncomment the last three lines to show the locations of the points. % (ja.doukaku.org Q65) rg = ceil(sqrt(num/pi)); [x y] = meshgrid(-rg:rg); pts = [x(:) y(:) sqrt(x(:).^2+y(:).^2) atan2(-y(:),-x(:))]; r = sortrows(pts, [3,4]); r = r(1:num,1:2); % figure % axis([-rg rg -rg rg]) % text(r(:,1),r(:,2),num2cell(1:num))