Intersecting rectangles coordinates (one rectangle can be contained within the other)

7 ビュー (過去 30 日間)
vivek
vivek 2015 年 6 月 29 日
コメント済み: Posada 2017 年 5 月 21 日
I have 2 position vectors[x,y,width,height] representing 2 rectangles.If they are intersecting , what is the easiest method to find the coordinates of the intersected area.Is there a matlab function to do this ? I know rectint returns the area but i want the coordinates of the rectangle
  1 件のコメント
Dan
Dan 2016 年 6 月 27 日
use the bBoxOverlapRatio() function or portions of it if youd like.

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

回答 (2 件)

Kelly Kearney
Kelly Kearney 2015 年 6 月 29 日
Assuming you already know the two polygons intersect, then you just need to compare where the left/right/bottom/top edges are:
r1 = [x1 y1 w1 h1];
r2 = [x2 y2 w2 h2];
l = max(x1,x2);
r = min(x1+w1,x2+w2);
b = max(y1,y2);
t = min(y1+h1,y2+h2);
r3 = [l r r-l t-b];
  3 件のコメント
Kelly Kearney
Kelly Kearney 2015 年 6 月 29 日
If you're interested in quantifying different amounts of overlap (for example, where exactly 3 polygons overlap, vs 2, 4, 5, etc), you might want to look at this function of mine: multiplepolyint.m. It might be overkill for axis-aligned rectangles, but it does the job pretty efficiently, especially with the fast flag turned on.
Posada
Posada 2017 年 5 月 21 日
Thanks was helpful, but actually r3 should be: r3 = [l b r-l t-b];

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


Brian Hannan
Brian Hannan 2015 年 6 月 29 日
編集済み: Brian Hannan 2015 年 6 月 29 日
I noticed from your comment above that you want to find the area of all of the intersecting rectangles rather than the coordinates of their intersection points. If that's the case, you can use polybool to do an AND operation on all of the polygons and then use polyarea to get the area of the result. This requires the mapping toolbox. If you don't have the mapping toolbox, you can use Emmett's Polygon Clipping and Offsetting filexchange submission .

カテゴリ

Help Center および File ExchangePoint Cloud Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by