两个矩形重叠多少? [英] How much do two rectangles overlap?

查看:29
本文介绍了两个矩形重叠多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个矩形 a 和 b,它们的边平行于坐标系的轴.我有它们的坐标为 x1,y1,x2,y2.

I have two rectangles a and b with their sides parallel to the axes of the coordinate system. I have their co-ordinates as x1,y1,x2,y2.

我试图确定,它们不仅重叠,而且重叠多少?我试图弄清楚它们是否真的是同一个矩形,给予或占据一点回旋余地.那么它们的面积 95% 是一样的吗?

I'm trying to determine, not only do they overlap, but HOW MUCH do they overlap? I'm trying to figure out if they're really the same rectangle give or take a bit of wiggle room. So is their area 95% the same?

对计算重叠百分比有帮助吗?

Any help in calculating the % of overlap?

推荐答案

计算交点的面积,也是一个矩形:

Compute the area of the intersection, which is a rectangle too:

SI = Max(0, Min(XA2, XB2) - Max(XA1, XB1)) * Max(0, Min(YA2, YB2) - Max(YA1, YB1))

从那里计算并集的面积:

From there you compute the area of the union:

SU = SA + SB - SI

你可以考虑比例

SI / SU

(在完美重叠的情况下为 100%,降至 0%).

(100% in case of a perfect overlap, down to 0%).

这篇关于两个矩形重叠多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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