相交矩形总面积 [英] total area of intersecting rectangles

查看:96
本文介绍了相交矩形总面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种算法来解决这个问题: 鉴于2矩形交叉或重叠在一起的任何一个角落,我怎么确定的总面积为两个矩形不重叠(交叉点)区域?意交集区域必须被计算一次,无论是与第一个矩形,或与第二个。

I need an algorithm to solve this problem: Given 2 rectangles intersecting or overlapping together in any corner, how do I determine the total area for the two rectangles without the overlapped (intersection) area? Meaning the area of intersection has to be calculated once, either with the first rectangle, or with second one.

推荐答案

这很容易。第一计算坐标交点,这也是一个矩形的

That's easy. First compute coordinates of intersection, which is also a rectangle.

left = max(r1.left, r2.left)
right = min(r1.right, r2.right)
bottom = max(r1.bottom, r2.bottom)
top = min(r1.top, r2.top)

然后,如果交集不为空(左<右放;&安培;底部<顶),由两个矩形的公共面积减去它: r1.area + r2.area - intersection.area

Then, if intersection is not empty (left < right && bottom < top), subtract it from the common area of two rectangles: r1.area + r2.area - intersection.area.

PS我假设矩形的坐标轴一致,这是通常情况下。

PS I assume rectangles are aligned by the coordinate axes, that's usually the case.

这篇关于相交矩形总面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆