What is an algorithm for determining the total area of two rectangles that are intersecting and may be rotated off the coordinate axes?
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.
Here's roughly what you need to do, expressed as generally as possible, but covering all possibilities:
Here's all the ways the rectangles can intersect:
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.
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:
So it goes like this:
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:
The area of a triangle is given by Heron formula:
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 ?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With