Area of Intersection of Two Rotated Rectangles

15,115

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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
    

Solution 5

Take each line segment of each rectangle and see if they intersect. There will be several possibilities:

  1. If none intersect - shared area is zero - unless all points of one are inside the other. In that case the shared area is the area of the smaller one.

  2. a If two consecutive edges of one rectactangle intersect with a single edge of another rectangle, this forms a triangle. Compute its area.

    b. If the edges are not consequtive, this forms a quadrilateral. Compute a line from two opposite corners of the quadrilateral, this makes two triangles. Compute the area of each and sum.

  3. If two edges of one intersect with two edges of another, then you will have a quadrilateral. Compute as in 2b.

  4. If each edge of one intersects with each edge of the other, you will have an octagon. Break it up into triangles ( e.g. draw a ray from one vertex to each other vertex to make 4 triangles )

@edit: I have a more general solution.

Check the special case in 1.

Then start with any intersecting vertex, and follow the edges from there to any other intersection point until you are back to the first intersecting vertex. This forms a convex polygon. draw a ray from the first vertex to each opposite vetex ( e.g. skip the vertex to the left and right. ) This will divide it into a bunch of triangles. compute the area for each and sum.

Share:
15,115
Nate Thorn
Author by

Nate Thorn

Updated on June 06, 2022

Comments

  • Nate Thorn
    Nate Thorn almost 2 years

    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.