How to create a circle filled with equidistant points inside it?

조회 수: 10(최근 30일)
Rushil Asthana
Rushil Asthana 2021년 10월 6일
댓글: Chunru 2021년 10월 7일
I want to create circle filled with equidistant points inside it. I have tried making the following program but it's taking too long to give output
radius = 100;
xc = 1;
yc = xc+1;
jc= 1;
jy=jc+1;
area = pi*radius*radius;
for i=xc:yc:area
theta = i*(2*pi);
r = sqrt(area)*radius;
for j=jc:jy:i
x = xc + r.*cos(theta);
y = yc + r.*sin(theta);
end
plot(x,y,'.')
end
  댓글 수: 5
Rushil Asthana
Rushil Asthana 2021년 10월 6일
@John D'Errico What will the code look like essentially? Cause I need help with exactly what you mentioned.

댓글을 달려면 로그인하십시오.

채택된 답변

Chunru
Chunru 2021년 10월 7일
편집: Chunru 2021년 10월 7일
Here is a sub-optimal (or almost optimal) solution. Filled dots will form the hexagonal formation.
radius = 10;
d = 1; % distance
xall=[]; yall=[];
dy = sqrt(3)/2 * d;
ny = floor(radius/dy);
for i=-ny:ny
y = dy*i;
if rem(i, 2)==0
nx = floor((sqrt(radius^2 - y^2))/d);
x = (-nx:nx)'*d;
else
nx = floor((sqrt(radius^2 - y^2)-d/2)/d);
x = (-nx-0.5:nx+0.5)'*d;
end
xall = [xall; x];
yall = [yall; y*ones(size(x))];
end
plot(xall(:), yall(:), '.');
hold on
theta = 0:360;
plot(radius*cosd(theta), radius*sind(theta), 'r')
axis equal
  댓글 수: 3
Chunru
Chunru 2021년 10월 7일
@Image Analyst Thanks for cultivating the good community culture. :-)

댓글을 달려면 로그인하십시오.

추가 답변(0개)

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by