# How to say the third selected point lies within the created geometry or not?

4 ビュー (過去 30 日間)
Kaleesh Bala 2018 年 7 月 24 日
コメント済み: Kaleesh Bala 2018 年 7 月 26 日
close all
clc
base_x = [0 50 50 20 0 0]
base_y = [0 0 10 10 10 0 ]
roof_x = [ 0 0 20 20 30 30]
roof_y =[ 10 50 10 50 50 10]
figure
plot(base_x, base_y,'-')
hold on
plot(roof_x, roof_y,'-')
axis([0 50 0 50])
grid on
[X,Y]=meshgrid(1:11);
[x] = ginput(2);
[x] = round(x);
fprintf ('the a1 coordinate value is %d,\n' , x(1,1));
fprintf ('the b1 coordinate value is %d ,\n' , x(1,2));
fprintf ('the a2 coordinate value is %d,\n' , x(2,1));
fprintf ('the b2 coordinate value is %d ,\n' , x(2,2));
How to say the third selected points a1,b1 a2,b2 lies within the created geometry ( base,roof) or not?

#### 4 件のコメント

Kaleesh Bala 2018 年 7 月 25 日
This is the geometry , a polygonal base_x = [0 50 50 20 0 0]
base_y = [0 0 10 10 10 0 ]
roof_x = [ 0 0 20 20 30 30]
roof_y =[ 10 50 10 50 50 10]
Walter Roberson 2018 年 7 月 25 日
Adam Danz 2018 年 7 月 25 日
Kaleesh, I'm continuing your other thread (link above) here because this topic seems to be more relevant to the problems you're having.

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

### 採用された回答

Adam Danz 2018 年 7 月 24 日

If I understand your question correctly, you're trying to determine if a coordinate (x,y) lies within a polygon.
First try matlab's inpolygon().
Another way to achieve that is by "ray casting". The idea is to draw a line from the (x,y) coordinate and extend it rightward until it passes all of the possible polygon edges. Then count the number of times that ray intersects with an edge of the polygon. If that number is non-zero and odd, then the coordinate (x,y) is within the polygon.
To achieve that, you'll need to loop through each edge of your polygon and determine if each line segment intersects with the virtual ray. Here's a nice tutorial on how to detect line segment intersection. And here's more info on the ray casting technique.

#### 5 件のコメント

Adam Danz 2018 年 7 月 25 日
Kaleesh, inpolygon() does work.
To test if the 2 mouse-click coordinates are within the lower rectangle,
[in1 on1] = inpolygon(x(:,1), x(:,2), base_x, base_y);
and to the other shape
[in2 on2] = inpolygon(x(:,1), x(:,2), roof_x, roof_y)
Two pieces of advice. First, make your lines thicker so you can see where you shapes are. Use the 'linewidth' parameter.
plot(base_x, base_y,'-', 'LineWidth', 3)
Second, the 2nd shape you're drawing doesn't have a bottom edge. inpolygon() will still work, though.
Kaleesh Bala 2018 年 7 月 25 日
yeah the bottom edge wasn't mentioned in the previous code,but there in the updated one
xv = [0 0 0 20 20 0 10 20 20 20 20 30 30 50 50 20 30 40 50]
yv = [0 0 50 10 0 0 0 0 0 10 50 50 10 10 0 0 0 0 0 ]
let me try to check with these values now Danke /Thanks Adam
Kaleesh Bala 2018 年 7 月 26 日
yeah the command worked ,even the previous one also works. for the further case just added frpintf by calling on && in with values to check

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

### Community Treasure Hunt

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

Start Hunting!

Translated by