points lying inside a polygon and a circle

How can I find the number of point lies inside both a circle and a polygon (the point should lie in the mutual/overlapping area in between the circle and the polygon) ?

 採用された回答

Matt J
Matt J 2017 年 1 月 30 日
編集済み: Matt J 2019 年 4 月 25 日

0 投票

If the circle is centered at (x0,y0), with radius R and the polygon has vertices xv,yv, you could do,
count = sum( inpolygon(x,y,xv,yv) & ((x-x0).^2+(y-y0).^2<=R^2) )

3 件のコメント

KalMandy
KalMandy 2017 年 1 月 30 日
what is the output of (x-x0).^2+(y-y0).^2<=R^2) ? does it become 1, if the point is inside the circle?
Matt J
Matt J 2017 年 1 月 30 日
Yes.
KalMandy
KalMandy 2017 年 1 月 30 日
Thanks a lot!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 1 月 30 日

2 投票

Did you try
count = 0;
if inpolygon(x,y,xCircle,yCircle) && inpolygon(x,y,xPoly,yPoly)
count = count + 1
end
where you call inpolygon twice, once passing in the circle coordinates and once passing in the polygon coordinates and then ANDing the two results together?

6 件のコメント

KalMandy
KalMandy 2017 年 1 月 30 日
can I use inpolygon for a circle as well?
Image Analyst
Image Analyst 2017 年 1 月 30 日
編集済み: Image Analyst 2017 年 1 月 30 日
Yes, if you have the coordinates for the perimeter of the circle. If you don't, then see Matt J's formula below. You didn't specify in what form you have information about the circle. You'd put my code in a loop over all (x,y) points that you have.
count = 0;
for k = 1 : length(x)
if inpolygon(x(k),y(k),xCircle,yCircle) && inpolygon(x(k),y(k),xPoly,yPoly)
count = count + 1
end
end
Same for Matt's code - loop over all x,y that you have.
Matt J
Matt J 2017 年 1 月 30 日
Same for Matt's code - loop over all x,y that you have.
Neither approach requires a loop over x,y. INPOLYGON is vectorized.
Image Analyst
Image Analyst 2017 年 1 月 30 日
You're right. x and y can be a vector of coordinates. Here's a demo:
% Make circle.
x0 = 20;
y0 = 30;
R = 15;
% Plot circle
pos = [x0-R, y0-R, 2*R, 2*R];
rectangle('Position', pos, 'Curvature',[1 1]);
hold on;
% Make polygon
xv = [10, 30, 30, 10, 10];
yv = [40, 40, 5, 5, 40];
plot(xv, yv, 'b-');
grid on;
% Make 60 points for x and y
numPoints = 60;
x = 70 * rand(1, numPoints);
y = 70 * rand(1, numPoints);
plot(x, y, 'r*');
% Now count how many are in both the circle and the polygon.
count = sum(inpolygon(x,y,xv,yv) & ((x-x0).^2+(y-y0).^2<=R^2))
KalMandy
KalMandy 2017 年 1 月 30 日
I have the radius and the coordinates of the center of the circle. what is given by xCircle,yCircle? Thanks for your response.
hanif hamden
hanif hamden 2019 年 4 月 25 日
If i put (x,y) in geographical coordinate and I want my radius is 10km. How should I do that?

サインインしてコメントする。

タグ

質問済み:

2017 年 1 月 30 日

編集済み:

2019 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by