How to check the number of points covered by a sector of a circle?
1 view (last 30 days)
Show older comments
This is the code written but i am not getting the answer right. Please help.
x=[2 2 2 3 3 5 6];
y=[4 7 8 1 4 7 5];
N=[x' y'];
axis([0 10 0 10]);
hold on;
scatter(x,y, [], 'filled');
%Labelling the nodes
labels = cellstr( num2str([1:length(x)]') );
plot(N(:,1), N(:,2), 'bx')
text(N(:,1), N(:,2), labels, 'VerticalAlignment','bottom', ...
'HorizontalAlignment','right')
%To draw the sector
delta=pi/2;
r=[1.5 1.5 1.5 1.5 1.5 1.5 2.5 2.5 2.5 2.5];
for i=1:length(x)
thetaN(i)=2*pi*0.4;
theta2(i) = thetaN(i) + delta/2;
t = linspace(thetaN(i),theta2(i));
A = x(i) + r(i)*cos(t);
B = y(i) + r(i)*sin(t);
AA=[x(i),A,x(i)];
BB=[y(i),B,y(i)];
plot(AA,BB,'b-');
end
%Points
vx=[1 1 1 2 2 2 3 3 3 4 4 4 5 5 5];
vy=[2 3 4 1 5 6 2 3 5 1 6 7 1 8 9];
V=[(vx)' (vy)'];
%Labelling the points
labels = cellstr( num2str([1:length(V)]') );
plot(V(:,1), V(:,2), 'rx')
text(V(:,1), V(:,2), labels, 'VerticalAlignment','bottom', ...
'HorizontalAlignment','right')
%To find the points covered
z=inpolygon(vx,vy,AA,BB)
Accepted Answer
Daniel Blight
on 6 Jul 2018
At end of the code you use "z=inpolygon(vx,vy,AA,BB)", here AA and BB have the values you gave them in the last iteration of the for loop (i.e. they plot sector 7). this means that inpolygon is only detecting points in sector 7 which means the answer of
z =
1×15 logical array
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
makes sense as point number 11 is contained within sector 7.
3 Comments
Daniel Blight
on 9 Jul 2018
Edited: Daniel Blight
on 9 Jul 2018
You'd need to evaluate inpolygon inside your for loop (this also means you need to move the %Points section to before the for loop) and store the answer at each iteration. One way would be to create a matrix at the start
Z = zeros(length(x),length(vx));
and then in the for loop (after you define AA and BB) put
Z(i,:)= inpolygon(vx,vy,AA,BB);
That way the ith line of Z would correspond to the points contained in sector i.
More Answers (1)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!