Number of grid points inside circle

조회 수: 16 (최근 30일)
Marina Markaki
Marina Markaki 2021년 4월 30일
편집: Scott MacKenzie 2021년 5월 5일
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에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by