how to use inpolygon function

9 Ansichten (letzte 30 Tage)
Sososasa
Sososasa am 30 Mär. 2014
Kommentiert: Sososasa am 30 Mär. 2014
Hi,
I have two polygons with different number of vertices:
polygon1= [0.1624 -0.6477;
0.2247 -0.6477;
0.2247 0.3874;
0.1624 0.5219]
polygon2= [0.1624 0.5219;
0.2247 0.3874;
0.2247 0.5651]
Where the first column is X-coordinates and the second column in Y-coordinates. I want to know if a any random point is in the first or second polygon. How can I do that? I read the documentation of this function (inpolygon) but I don't understand it well.

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 30 Mär. 2014
A polygon is defined by its vertices(corner points), being pairs of (x,y) coordinates. In your case, these coordinates are stored in a single N-by-2 matrix.
polygon1= [0.1624 -0.6477;
0.2247 -0.6477;
0.2247 0.3874;
0.1624 0.5219]
P = rand(1,2)/2 % we will look at 1 point, but in poly can accept multiple points!
tf = inpolygon(P(:,1), P(:,2),polygon1(:,1), polygon1(:,2))
plot(polygon1([1:end 1],1), polygon1([1:end 1],2),'b.-',P(:,1), P(:,2),'r*') % a plot is often informative
if any(tf==1),
title('Some point(s) is (are) in or on the edge of polygon 1') ;
else
title('All points are outside polygon 1') ;
end
I hope this helps.
  1 Kommentar
Sososasa
Sososasa am 30 Mär. 2014
Thank you a lot that really help :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by