Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

total area of intersecting rectangles

Tags:

algorithm

What is an algorithm for determining the total area of two rectangles that are intersecting and may be rotated off the coordinate axes?

like image 478
lolo Avatar asked Dec 29 '10 23:12

lolo


People also ask

What is the area of overlap?

The OverlapArea function is a Spatial Numeric measurement that calculates the total area (or length or count) of overlap between features in the current layer and features in the target layer.


2 Answers

Here's roughly what you need to do, expressed as generally as possible, but covering all possibilities:

  • Work out the class of intersection. I.e. How many edges does the intersection area have? It could be anything from 0 to 8.
  • Find all vertices of the intersection. This will be all intersections between the edges of the rectangles, and associated corners of the rectangles themselves. Working this bit out is the most complex/tedious.
  • Work out the area of the intersection, by dividing it up into triangles if necessary.

Here's all the ways the rectangles can intersect: alt text

Update

I've had some thoughts, and the best way to categorize the intersection is to trace round the perimeter of each rectangle and count the number of times each edge intersects another edge. You'll get a vector, e.g. for a six sided intersection area: {1,1,1,1},{0,1,1,1}, and for 8: {2,2,2,2},{2,2,2,2}. The two special cases you'll need to check for are when one rectangle completely encloses the other and when the edges are in line. You'll need to do careful checks, but this would be the starting point for a function to categorize the intersection.

like image 92
fredley Avatar answered Sep 21 '22 20:09

fredley


Area(R1 union R2) = Area(R1) + Area(R2) - Area(R1 intersection R2), so you can calculate the area of the intersection to have the area of the union.

Intersections of two rectangles (or two convex polygons) are simple:

  • They are convex polygons
  • Their points are characterized as follows: intersection points of any pair of edges, and points of one rectangle which are inside the other.

So it goes like this:

  • L is an initially empty linked list
  • R1 has edges e1, e2, e3, e4, R2 has edges f1, f2, f3, f4. Compute intersection points of ei and fj, for all i,j=1,2,3,4. Add them to list L.
  • For each vertex v of R1, if v is inside R2, add it to L.
  • For each vertex w of R2, if w is inside R1, add it to L.

The convex hull of points in L is your intersection. As every point in L is on the boundary of the intersection, you can triangulate it and compute its area. Easy:

  • L = [x0, x1, ... ]
  • Sort points in L according to the angle of (xi - x0) with respect to an horizontal line passing through x0
  • First triangle is x0, x1, x2
  • Second triangle is x0, x2, x3
  • nth triangle is x0, x(n+1), x(n+2)

The area of a triangle is given by Heron formula:

  • a, b, c are edge lengths
  • s = 0.5 * (a + b + c)
  • area = sqrt(s * (s - a) * (s - b) * (s - c))

but beware of computing s - a, s - b and s - c independantly since you can encounter roundoff error (what if c ~ a and b << a for instance ?)

like image 37
Alexandre C. Avatar answered Sep 18 '22 20:09

Alexandre C.