Number of grid points inside circle
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marina Markaki
am 30 Apr. 2021
Bearbeitet: Scott MacKenzie
am 5 Mai 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 Kommentar
Star Strider
am 30 Apr. 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)]
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')
Is there a problem with what it does?
Akzeptierte Antwort
Scott MacKenzie
am 30 Apr. 2021
Bearbeitet: Scott MacKenzie
am 5 Mai 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 Kommentare
Scott MacKenzie
am 5 Mai 2021
Bearbeitet: Scott MacKenzie
am 5 Mai 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!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Elementary Polygons finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!