C++编程,求俩矩形重叠面积的代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++编程,求俩矩形重叠面积的代码相关的知识,希望对你有一定的参考价值。

就俩矩形给出俩坐标,既对角线坐标。求两个矩形重叠的面积。

第一个矩形左下角x1,y1,右上角x2,y2,第二个左下x3,y3,右上x4,y4:
假设两矩形相交,则相交区域的坐标为
左下角max(x1,x3),max(y1,y3)
右上角min(x2,x4),min(y2,y4)
要使条件成立,则min(x2,x4)-max(x1,x3)>=0 且min(y2,y4)-max(y1,y3)>=0

如果假设成立,则相交矩形面积为:(min(x2,x4)-max(x1,x3))* (min(y2,y4)-max(y1,y3))

代码如下:

int max(int a,int b)

return a>b?a:b ;

int min(int a ,int b)

return a<b?a:b ;


int GetIntersectArea(int x1,int x2,int x3,int x4,int y1,int y2,int y3,int y4)

if((min(x2,x4)-max(x1,x3)>=0)&& (min(y2,y4)-max(y1,y3)>=0))

return (min(x2,x4)-max(x1,x3))* (min(y2,y4)-max(y1,y3));

else

printf("矩形不相交\n");

return 0;
参考技术A 你是想求两个矩形目标区域的overlap吧?这边有个matlab代码,你稍稍改动一下就好了。。里面只用到了max,和min库函数:
function ratio = overlap(Rectan_A,Rectan_B)
x1 = Rectan_A(1);
y1 = Rectan_A(2);
width1 = Rectan_A(3);
height1 = Rectan_A(4);

x2 = Rectan_B(1);
y2 = Rectan_B(2);
width2 = Rectan_B(3);
height2 = Rectan_B(4);

startx = min(x1, x2); endx = max(x1+width1, x2+width2);
over_width = width1 + width2 - (endx - startx);

starty = min(y1, y2); endy = max(y1 + height1, y2 + height2);
over_height = height1+height2-(endy-starty);

if over_width< 3.0009e-04||over_height< 3.0009e-04
rario = 0;
else
Area_over = over_width*over_height;
Area1 = width1*height1;
Area2 = width2*height2;
ratio = Area_over/(Area1+Area2-Area_over);
end
参考技术B #include <stdio.h>
void order_swap(float * a, float * b)

float c = 0; if (!a || !b) return;
if(*a < *b) return ;
c = *b; *b = *a; *a = c;

int main(void)

float x0,y0,x1,y1,x2,y2,x3,y3;
printf("x0,y0?"); scanf("%f %f", &x0, &y0);
printf("x1,y1?"); scanf("%f %f", &x1, &y1);
printf("x2,y2?"); scanf("%f %f", &x2, &y2);
printf("x3,y3?"); scanf("%f %f", &x3, &y3);
order_swap(&x0, &x1); order_swap(&y0, &y1);
order_swap(&x2, &x3); order_swap(&y2, &y3);
if(x1 < x2 || y1 < y2)
printf("S = 0\n");
else
printf("S = %f\n", (x1 - x2) * (y1 - y2));
return 0;
参考技术C 不同意楼上的,首先人家说的是c++,你那搞个C不说,还不是纯正的C!第二,两个矩形相交的情况是在是太多了,交出来有可能是两对角互补的针形,有可能是三角形、梯形,甚至是一个包涵在另一个里头的样子……
所以不知道,题目还有没有其他的说明,不然的话,那分的情况就太多了,要很多次的判断!!

Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)

 

Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)

 

 

 

 1 function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, BBox_y2, BBox_gt_x1, BBox_gt_y1, BBox_gt_x2, BBox_gt_y2)
 2 
 3     x1 = BBox_x1;
 4     y1 = BBox_y1;
 5     width1  = BBox_x2 - BBox_x1;
 6     height1 = BBox_y2 - BBox_y1;
 7 
 8     x2 = BBox_gt_x1;
 9     y2 = BBox_gt_y1;
10     width2  = BBox_gt_x2 - BBox_gt_x1;
11     height2 = BBox_gt_y2 - BBox_gt_y1;
12 
13     endx     = math.max(x1+width1, x2+width2);
14     startx     = math.min(x1, x2);
15     width     = width1 + width2 - (endx - startx);
16 
17     endy = math.max(y1 + height1, y2 + height2);
18     starty = math.min(y1, y2);
19     height = height1 + height2 - (endy - starty);
20 
21     if width<=0 or height<=0 then 
22         ratio = 0;
23     else
24         Area = width*height;
25         Area1 = width1*height1;
26         Area2 = width2*height2;
27         ratio = Area/Area1;
28     end
29 
30     return ratio 
31 
32 end 

 

以上是关于C++编程,求俩矩形重叠面积的代码的主要内容,如果未能解决你的问题,请参考以下文章

C++编程!已知矩形,判断输入的点是不是包含在该矩形内。

用C++编程求解圆,长方形,正方形等面积

C++编程:结构体类型编程实例“求圆形水塘甬道面积及栅栏长度

C++编程练习:抽象类——编写一个程序,计算三角形正方形的面积,抽象出一个基类base。

python跟c++区别在哪里

Overlapping rectangles判断两个矩形是否重叠的问题 C++