Number of grid points inside circle

30 visualizaciones (últimos 30 días)
Marina Markaki
Marina Markaki el 30 de Abr. de 2021
Editada: Scott MacKenzie el 5 de Mayo de 2021
I have my radius being between 1 and 10 and my angle from 0 to 2pi. I defined an uniform rectangular grid with 10 points (10 by 10) and I want to find how many points fall into the circle. This is my code but when I run it I do not get the figure that I want. Thanks a lot.
xq=-10:10;
yq=-10:10;
a=0:0.01:2*pi; %angle
r = 10; % radius
xv = r*cos(a); % cartesian x coordinate
yv = r*sin(a); % cartesian y coordinate
in = inpolygon(xq,yq,xv,yv);
figure(1)
plot(xv,yv) % circle
axis equal
hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off
  1 comentario
Star Strider
Star Strider el 30 de Abr. de 2021
The code you posted —
xq=-10:10;
yq=-10:10;
a=0:0.01:2*pi; %angle
r = 10; % radius
xv = r*cos(a); % cartesian x coordinate
yv = r*sin(a); % cartesian y coordinate
in = inpolygon(xq,yq,xv,yv);
figure(1)
plot(xv,yv) % circle
axis equal
hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off
The code I posted previously in Find all the grid points that are inside of the circle does this differently —
r = 1:10;
% r = pi*[1:3];
a = linspace(0, 2*pi);
x = r(:)*cos(a)+5.5;
y = r(:)*sin(a)+5.5;
[Xg,Yg] = ndgrid(1:numel(r));
Xv = Xg(:);
Yv = Yg(:);
for k = 1:size(Xg,1)
[incirc(:,k),oncirc(:,k)] = inpolygon(Xv,Yv,x(k,:),y(k,:));
end
circtot = [nnz(incirc) nnz(oncirc)]
circtot = 1×2
676 0
CircleRad = 3;
figure
hp{1} = plot(x.', y.', '--b');
hold on
hp{2} = plot(Xv, Yv, '.k');
hp{3} = plot(Xv(incirc(:,CircleRad)), Yv(incirc(:,CircleRad)), 'or');
hp{4} = plot(Xv(oncirc(:,CircleRad)), Yv(oncirc(:,CircleRad)), 'og');
hold off
axis('equal')
text(cosd(30)*r+5.5, sind(30)*r+5.5,compose('$%d$',r), 'Horiz','center','Vert','middle', 'Interpreter','latex')
legend([hp{1}(1),hp{2}(1),hp{3}(1)],'Circles', 'Grid', sprintf('In Circle r = %d (N = %d)', CircleRad, nnz(incirc(:,CircleRad))), sprintf('On Circle r = %d (N = %d)', CircleRad, nnz(oncirc(:,CircleRad))), 'Location','best')
Warning: Ignoring extra legend entries.
Is there a problem with what it does?

Iniciar sesión para comentar.

Respuesta aceptada

Scott MacKenzie
Scott MacKenzie el 30 de Abr. de 2021
Editada: Scott MacKenzie el 5 de Mayo de 2021
I only changed the 1st two lines. Is this what you are looking for?
xq = repmat(-10:10,21,1);
yq = xq';
a = 0:0.01:2*pi; % angle
r = 10; % radius
xv = r*cos(a); % cartesian x coordinate
yv = r*sin(a); % cartesian y coordinate
in = inpolygon(xq,yq,xv,yv);
figure(1)
plot(xv,yv) % circle
axis([-11 11 -11 11]);
hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off
  2 comentarios
Scott MacKenzie
Scott MacKenzie el 5 de Mayo de 2021
Editada: Scott MacKenzie el 5 de Mayo de 2021
Marina: Oops, that was my mistake. I just corrected the solution. I changed the first line from
xq = repmat(-10:10,10,1); % Oops. Wrong number of repetitions of the vector
to
xq = repmat(-10:10,21,1); % 21 repetitions needed!
Marina Markaki
Marina Markaki el 5 de Mayo de 2021
I just wrote xq = repmat(-10:10,100) and I got exactly the figure that I wanted. Thank you a lot for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by