如何计算重叠面积? [英] How to calculate the overlap area?

查看:210
本文介绍了如何计算重叠面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个重叠的矩形,我需要计算重叠区域(宽度和高度).请这张图片:

There are two overlapping rectangles and I need to calculate the overlap area (width and height). Please this image:

推荐答案

如果矩形r1位于x1,y1并具有宽度w1,h1,同样,矩形r2位于x2,y2且具有宽度w2和高度h2,那么您可以找到红色区域的左边缘,例如(假设两个矩形的宽度和高度均为正,因此位置为左下角):

If rectangle r1 is at x1,y1 and have width w1,h1, and likewise rectangle r2 is at x2,y2 with width w2 and height h2, then you can find the left edge of the red region like (Assuming the widths and heights of both rectangles are positive so the positions are the bottom left corners):

left = max(x1, x2);

类似地,在右侧,底部和顶部:

Similarly for the right, bottom and top:

right = min(x1 + w1, x2 + w2);
bottom = max(y1, y2);
top = min(y1 + h1, y2 + h2);

重叠区域的大小为

height = top - bottom 
width = right - left. 

如果这些都是负数,则没有重叠.

If either if these is negative, there is no overlap.

这篇关于如何计算重叠面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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