C语言 两个矩形的合并

版权声明:转载需标明该文链接。 https://blog.csdn.net/zaibeijixing/article/details/78045511
function:仿opencv中 rect01 & rect02
不重叠矩形的合并及面积.#include <stdio.h>
#define max(x,y)  ( x>y?x:y )
#define min(x,y)  ( x<y?x:y )

typedef struct  ss
{
	int a;
	int b;
}ss;
ss h;

typedef struct tag_rect_s
{
	int left;
	int right;
	int top;
	int bottom;
}Rect_s;


/************************************************************************************************************************************************************
* function : rect_or()
* return to a big_rect;
************************************************************************************************************************************************************/
Rect_s RectOr(Rect_s &a, Rect_s &b)
{
	int left = min(a.left, b.left);
	int right = max(a.right, b.right);
	int top = min(a.top, b.top);
	int bottom = max(a.bottom, b.bottom);

	Rect_s rect_or = Rect_s{ left ,right ,top ,bottom };
	return rect_or;
}


/************************************************************************************************************************************************************
* function : RectAarea()

************************************************************************************************************************************************************/

int RectAarea(Rect_s &a)
{
	return ((a.right - a.left)*(a.bottom - a.top));
}


int main()
{
	//ss h;
	h = ss{5,7};
	printf("\n %d %d\n", h.a,h.b);


	//Rect_s rect01 = Rect_s{ 10,100,10,100 };
	//Rect_s rect01 = { 10,100,10,100 };
	Rect_s rect01{ 10,100,10,100 };
	
	Rect_s rect02 = Rect_s{ 150,300,120,200 };
	Rect_s rect03 = rect01;

	Rect_s rect_or = RectOr(rect03, rect02);
	printf("\n L=%d R=%d T=%d B=%d\n", rect_or.left, rect_or.right, rect_or.top, rect_or.bottom);

	int area=RectAarea(rect_or);
	printf("\n area_or = %d\n", area);


	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zaibeijixing/article/details/78045511