Borrar filtros
Borrar filtros

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

35 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 6 de Oct. de 2021
Editada: Stephen23 el 6 de Oct. de 2021
@Rushil Asthana: because you did not mention anything about an optimal solution or any specific boundary requirements then this task is trivially easy: first generate a square grid of points large enough to cover the required circle, then simply discard those outside the circle. Reasonably efficient and should take only a few lines of code.
That will provide you with exactly what you asked for: points with exactly equal spacing all within a circle.
Rushil Asthana
Rushil Asthana el 6 de Oct. de 2021
@John D'Errico What will the code look like essentially? Cause I need help with exactly what you mentioned.

Iniciar sesión para comentar.

Respuesta aceptada

Chunru
Chunru el 7 de Oct. de 2021
Editada: Chunru el 7 de Oct. de 2021
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 comentarios
Image Analyst
Image Analyst el 7 de Oct. de 2021
@Rushil Asthana, then could you please click the "Accept this answer" link to grant @Chunru the reputation points he deserves for helping you. Thanks in advance. 🙂
Chunru
Chunru el 7 de Oct. de 2021
@Image Analyst Thanks for cultivating the good community culture. :-)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by