inpolygon command for multiple areas

5 ビュー (過去 30 日間)
tafteh
tafteh 2012 年 12 月 4 日
Hi all,
In part of my code I am intrested in realizing whether a point = (x,y) is withing multiple areas or not. Here, each area is a polygon (simply a square) that have the coordination of its center locater which I can calcuate the corner points.
I learnt that by using "inpolygon" comman I can realize whether my point is withing that polygon(or on its edge). Hoever, I am interested to recognize what area whose the point is withing.
i.e. I have area1, area2 and area3.
I set following variables:
% definig all the areas coordination
xv = [1 2 2 1 1 NaN 4 4 5 5 4 NaN 6 6 7 7 6];
yv = [0 0 3 3 0 NaN 1 2 2 1 1 NaN 4 5 5 4 4];
x = 1.5;
y = 3;
in = inpolygon(x,y,xv,yv);
I can find that my point is within one of the three defined areas (Here is within the area1). But how can I say the point is within what area specifically?
I would appreciate if get some help in this,
Best

採用された回答

Kelly Kearney
Kelly Kearney 2012 年 12 月 4 日
This should do it:
Note that that function expects polygons to be defined with clockwise vertices (counterclockwise is reserved for holes), so you'd have to preprocess your vertices:
xv = [1 2 2 1 1 NaN 4 4 5 5 4 NaN 6 6 7 7 6];
yv = [0 0 3 3 0 NaN 1 2 2 1 1 NaN 4 5 5 4 4];
x = 1.5;
y = 3;
[xv, yv] = poly2cw(xv, yv);
[in, idx] = inpolygons(x, y, xv, yv]
  4 件のコメント
Kelly Kearney
Kelly Kearney 2012 年 12 月 14 日
Sorry, I always forget that the polygon tools (ispolycw, poly2cw, polybool, etc) are Mapping Toolbox functions. They really ought to move those to the base toolbox; I would think geometric calculations like that would be useful for non-mapping purposes.
Anyway, in answer to your question, inpolygons is basically just a glorified loop over the inpolygon function. I included specific restrictions on orientation so it could be used with shapefile-derived polygons, but for your purposes, this probably isn't necessary. You can probably modify my code accordingly; just break your initial vectors based on the NaNs, then loop over each polygon with inpolygon.
tafteh
tafteh 2012 年 12 月 21 日
Modified your code and its working!
Thanks again for your help:)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by