求两个矩形重叠部分的面积

11 篇文章 0 订阅
#include<stdio.h>
#include<math.h>

#define areaFile "area.txt"
#define perportionFile "perportion.txt"

#define min(a,b) ( ((a)>(b)) ? (b):(a) )
#define max(a,b) ( ((a)>(b)) ? (a):(b) )

typedef struct xy
{
int x;
int y;
}Point;

bool saveValueToFile(const char *fileName, double g[][22], int rows);

void main()
{
	//每一行的两个点分表表示一个矩形的左上角和右下角坐标
	Point A[] = {{73, 130}, {1498, 130},
	{303, 205}, {1269, 346},
	{275, 381}, {1304, 408},
	{139, 466}, {1434, 692},
	{140, 732}, {1212, 754},
	{436, 777}, {1138, 799},
	{75, 855}, {314, 878},
	{75, 902}, {769, 958},
	{73, 963}, {770, 1232},
	{73, 1237}, {770, 1537},
	{73, 1542}, {770, 1782},
	{74, 1841}, {770, 1907},
	{74, 1910}, {770, 1988},
	{73, 2001}, {768, 2043},
	{73, 2051}, {440, 2073},
	{73, 2076}, {770, 2124},
	{803, 901}, {1500, 1172},
	{803, 1177}, {1501, 1508},
	{803, 1515}, {1501, 1909},
	{803, 1913}, {1500, 2125},
	{655, 2150}, {914, 2166}};

	//每一行的两个点分表表示一个矩形的左上角和右下角坐标
	Point B[] = {{75, 72}, {1499, 90},
	{256, 147}, {1314, 275},
	{551, 333}, {1022, 362},
	{139, 424}, {1420, 560},
	{140, 567}, {1432, 731},
	{140, 771}, {1112, 793},
	{436, 814}, {1137, 836},
	{75, 889}, {318, 913},
	{74, 938}, {768, 995},
	{74, 1003}, {769, 1201},
	{74, 1287}, {769, 1760},
	{74, 1806}, {769, 1853},
	{107, 1878}, {644, 1905},
	{73, 1919}, {768, 1991},
	{73, 1994}, {769, 2067},
	{803, 937}, {1500, 1630},
	{837, 1633}, {1499, 1662},
	{804, 1666}, {1499, 1725},
	{938, 1762}, {1498, 1794},
	{803, 1825}, {1499, 1985},
	{946, 2018}, {1498, 2046},
	{200, 2087}, {1372, 2121}};

	double value[sizeof(A) / (2 * sizeof(A[0]))][sizeof(B) / (2 * sizeof(B[0]))];
	double perportion[sizeof(A) / (2 * sizeof(A[0]))][sizeof(B) / (2 * sizeof(B[0]))];
	int s, i, j, count = 0;
	//int chang, kuang;
	int width1, height1, width2, height2;
	Point a[4] = {0};

	printf("A rows: %d\n", sizeof(A)/(2 * sizeof(A[0])));
	printf("B rows: %d\n", sizeof(B)/(2 * sizeof(B[0])));

	for (i = 0; i < sizeof(A)/(sizeof(A[0])); i += 2)
	{
		a[0].x = A[i].x;
		a[0].y = A[i].y;
		a[1].x = A[i + 1].x;
		a[1].y = A[i + 1].y;

		width1 = abs(a[1].x - a[0].x);
		height1 = abs(a[1].y - a[0].y);

		for (j = 0; j < sizeof(B)/(sizeof(B[0])); j += 2)
		{
			a[2].x = B[j].x;
			a[2].y = B[j].y;
			a[3].x = B[j + 1].x;
			a[3].y = B[j + 1].y;

			width2 = abs(a[3].x - a[2].x);
			height2 = abs(a[3].y - a[2].y);
			
			s = 0;
			if (a[1].x < a[2].x || a[3].x < a[0].x || a[3].y < a[0].y || a[2].y > a[1].y)
			{
				s = 0;	//没有重叠区域
			}
			else
			{
				s = (min(a[3].y , a[1].y) - max(a[0].y , a[2].y)) * (min(a[3].x , a[1].x) - max(a[0].x , a[2].x));
			}

			/*
			chang=min(abs(a[1].x-a[0].x),abs(a[3].x-a[2].x));
			kuang=min(abs(a[1].y-a[0].y),abs(a[3].y-a[2].y));
			if(min(a[2].x,a[3].x)>max(a[0].x,a[1].x)||max(a[2].x,a[3].x)<min(a[0].x,a[1].x)||min(a[2].y,a[3].y)>max(a[0].y,a[1].y)||max(a[2].y,a[3].y)<min(a[0].y,a[1].y))
			{
				s=0;
			}
			else
			{
				s=min(min(abs(a[2].x-a[1].x),abs(a[3].x-a[0].x)),chang)*min(min(abs(a[2].y-a[1].y),abs(a[3].y-a[0].y)),kuang);
				printf("%d: A[%d] 与 B[%d]有重叠,面积%d\n", ++count, i / 2, j / 2, s);
			}
			*/

			value[i/2][j/2] = (double)s;
			perportion[i/2][j/2] = 1.0 - 2.0 * value[i/2][j/2] / (width1 * height1 + width2 * height2);
		}
	}

	saveValueToFile(areaFile, value, sizeof(A)/(2 * sizeof(A[0])));
	saveValueToFile(perportionFile, perportion, sizeof(A)/(2 * sizeof(A[0])));
	
#if 0
	while(true)
	{
		printf("Please input 4 x,y:for example 1,2\n");
		for(int i=0;i<4;i++)
		{
			scanf("%d %d",&a[i].x,&a[i].y);
		}
		chang=min(abs(a[1].x-a[0].x),abs(a[3].x-a[2].x));
		kuang=min(abs(a[1].y-a[0].y),abs(a[3].y-a[2].y));
		if(min(a[2].x,a[3].x)>max(a[0].x,a[1].x)||max(a[2].x,a[3].x)<min(a[0].x,a[1].x)||min(a[2].y,a[3].y)>max(a[0].y,a[1].y)||max(a[2].y,a[3].y)<min(a[0].y,a[1].y))
		{
			s=0;
		}
		else
		{
			s=min(min(abs(a[2].x-a[1].x),abs(a[3].x-a[0].x)),chang)*min(min(abs(a[2].y-a[1].y),abs(a[3].y-a[0].y)),kuang);
		}
		printf("%d\n",s);
	}
#endif
	/*
	while(true)
	{
		printf("Please input 4 x,y:for example 1,2\n");
		//注意我选取的坐标系和我们平时在数学里面遇到的一样,和你昨天晚上讨论的坐标系有点细微区别,如果映射到你的坐标系的话
		//将点的x与y坐标互换即可,在我的这个测试程序中输入左下角和右上角两个点的坐标确定一个矩形
		for(int i=0;i<4;i++)
		{
			scanf("%d %d",&a[i].x,&a[i].y);
		}

	
	 *第一个矩形左下角x1,y1,右上角x2,y2,第二个左下x3,y3,右上x4,y4
	 *1 当x2<x3 or x4<x1 or y4<y1 or y3>y2 没有交集
	 *2 除去1的情况后交集为  (min(y4,y2)-max(y1,y3))*(min(x4,x2)-max(x1,x3))
	 

		if (a[1].x < a[2].x || a[3].x < a[0].x || a[3].y < a[0].y || a[2].y > a[1].y)
		{
			s = 0;	//没有重叠区域
		}
		else
		{
			s = (min(a[3].y , a[1].y) - max(a[0].y , a[2].y)) * (min(a[3].x , a[1].x) - max(a[0].x , a[2].x));
		}

		printf("Overlapping area is: %d\n", s);
	}*/

}

bool saveValueToFile(const char *fileName, double g[][22], int rows)
{
	FILE *fp = NULL;
	int i, j;

	fp = fopen(fileName, "w");
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < 22; j++)
		{
			fprintf(fp, "%8.7lf ", g[i][j]);
		}
		fprintf(fp, "%s", "\n");
	}

	fflush(fp);
	fclose(fp);

	return true;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值