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

조회 수: 1 (최근 30일)
Aida Jones
Aida Jones 2018년 7월 6일
답변: Aida Jones 2018년 7월 9일
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)
  댓글 수: 2
KSSV
KSSV 2018년 7월 6일
Question is not clear.
Aida Jones
Aida Jones 2018년 7월 8일
I have plotted some points. I want every sector to detect what are the points it covers (detects).

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Blight
Daniel Blight 2018년 7월 6일
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
Daniel Blight
Daniel Blight 2018년 7월 9일
편집: Daniel Blight 2018년 7월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aida Jones
Aida Jones 2018년 7월 9일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by