Number of grid points inside circle

29 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2021-4-30
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?

请先登录,再进行评论。

采纳的回答

Scott MacKenzie
Scott MacKenzie 2021-4-30
编辑:Scott MacKenzie 2021-5-5
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 个评论
Scott MacKenzie
Scott MacKenzie 2021-5-5
编辑:Scott MacKenzie 2021-5-5
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 2021-5-5
I just wrote xq = repmat(-10:10,100) and I got exactly the figure that I wanted. Thank you a lot for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by