Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Area of Intersection of Two Rotated Rectangles

I have two 2D rectangles, defined as an origin (x,y) a size (height, width) and an angle of rotation (0-360°). I can guarantee that both rectangles are the same size.

I need to calculate the approximate area of intersection of these two rectangles. Rectangle intersection

The calculation does not need to be exact, although it can be. I will be comparing the result with other areas of intersection to determine the largest area of intersection in a set of rectangles, so it only needs to be accurate relative to other computations of the same algorithm.

I thought about using the area of the bounding box of the intersected region, but I'm having trouble getting the vertices of the intersected region because of all of the different possible cases: So many possible intersection shapes

I'm writing this program in Objective-C in the Cocoa framework, for what it's worth, so if anyone knows any shortcuts using NSBezierPath or something you're welcome to suggest that too.

like image 527
Nate Thorn Avatar asked Jul 26 '12 13:07

Nate Thorn


4 Answers

To supplement the other answers, your problem is an instance of line clipping, a topic heavily studied in computer graphics, and for which there are many algorithms available. If you rotate your coordinate system so that one rectangle has a horizontal edge, then the problem is exactly line clipping from there on.

You could start at the Wikipedia article on the topic, and investigate from there.

like image 66
Joseph O'Rourke Avatar answered Nov 13 '22 04:11

Joseph O'Rourke


A simple algorithm that will give an approximate answer is sampling.

Divide one of your rectangles up into grids of small squares. For each intersection point, check if that point is inside the other rectangle. The number of points that lie inside the other rectangle will be a fairly good approximation to the area of the overlapping region. Increasing the density of points will increase the accuracy of the calculation, at the cost of performance.

like image 24
Mark Byers Avatar answered Nov 13 '22 05:11

Mark Byers


In any case, computing the exact intersection polygon of two convex polygons is an easy task, since any convex polygon can be seen as an intersection of half-planes. "Sequential cutting" does the job.

Choose one rectangle (any) as the cutting rectangle. Iterate through the sides of the cutting rectangle, one by one. Cut the second rectangle by the line that contains the current side of the cutting rectangle and discard everything that lies in the "outer" half-plane.

Once you finish iterating through all cutting sides, what remains of the other rectangle is the result.

like image 4
AnT Avatar answered Nov 13 '22 05:11

AnT


You can actually compute the exact area.

  1. Make one polygon out of the two rectangles. See this question (especially this answer), or use the gpc library.
  2. Find the area of this polygon. See here.
  3. The shared area is

    area of rectangle 1 + area of rectangle 2 - area of aggregated polygon
    
like image 3
Shahbaz Avatar answered Nov 13 '22 05:11

Shahbaz