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 Center および File ExchangeElementary Polygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by