Area of Intersection of Two Rotated Rectangles

2020-02-20 08:17发布

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.

6条回答
来,给爷笑一个
2楼-- · 2020-02-20 08:26

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.

查看更多
一夜七次
3楼-- · 2020-02-20 08:33

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.

查看更多
SAY GOODBYE
4楼-- · 2020-02-20 08:34

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.

查看更多
欢心
5楼-- · 2020-02-20 08:39

A brute-force-ish way:

  • take all points from the set of [corners of rectangles] + [points of intersection of edges]
  • remove the points that are not inside or on the edge of both rectangles.
  • Now You have corners of intersection. Note that the intersection is convex.
  • sort the remaining points by angle between arbitrary point from the set, arbitrary other point, and the given point.
  • Now You have the points of intersection in order.
  • calculate area the usual way (by cross product)

.

查看更多
男人必须洒脱
6楼-- · 2020-02-20 08:48

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
    
查看更多
小情绪 Triste *
7楼-- · 2020-02-20 08:51

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.

查看更多
登录 后发表回答