How to check the number of points covered by a sector of a circle?
1 回表示 (過去 30 日間)
古いコメントを表示
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)
採用された回答
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
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 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!