展开全部
第一个矩形左下角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;
}
假设两矩形相交,则相交区域的坐标为
左下角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;
}
展开全部
你是想求两个矩形目标区域的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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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;
}
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!第二,两个矩形相交的情况是在是太多了,交出来有可能是两对角互补的针形,有可能是三角形、梯形,甚至是一个包涵在另一个里头的样子……
所以不知道,题目还有没有其他的说明,不然的话,那分的情况就太多了,要很多次的判断!!
所以不知道,题目还有没有其他的说明,不然的话,那分的情况就太多了,要很多次的判断!!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询