How to check the number of points covered by a sector of a circle?

1 view (last 30 days)
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
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
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.

Sign in to comment.

More Answers (1)

Aida Jones
Aida Jones on 9 Jul 2018
Ho do i write Z(i,:)=inpolygon(vx,vy,AA,BB) for the following code:
x=[1 1 2 2 3 3 3 3 4 5]; y=[1 3 2 3 1 2 6 8 6 8]; 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;
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
%To plot virtual sensors at the centroid of the fan
for i=1:length(x)
xV(i)=x(i) + ((2*r(i)/3)*cos((theta2(i)+thetaN(i))/2)*sin(delta/2)/(delta/2));
yV(i)=y(i) + ((2*r(i)/3)*sin((theta2(i)+thetaN(i))/2)*sin(delta/2)/(delta/2));
end
plot(xV,yV,'ro');
M=[(xV)' (yV)'];
%Voronoi for virtual sensors
voronoi(xV,yV,'green');
[vx,vy]=voronoi(xV,yV);
plot(vx,vy,'rx'); %vertex
grid on
[V C]=voronoin(M); % To determine V, the vertex location of Voronoi Cell (C)
%Labelling the vertices 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 = zeros(1,length(vx));
for i=1:length(x)
Z(i,:)=inpolygon(vx,vy,AA,BB)
end

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!