矩形重叠检测。

// 矩形重叠类型注释
// CORNER_OVERLAP
// --------------------
// |                  |
// |                  |
// |        **********|**********
// |        *         |         *
// |        *         |         *
// ---------*---------|         *
//          *                   *
//          *                   *
//          *********************
//  
//  ANCHOR_OVERLAP
//            ----
//            |  |
//            |  |
//     *******|**|*******
//     *	  |	 |		*
//     *      |  |      *
//     *      ----      *
//     *                *
//     ******************
//   SAME_WIDTH_OVERLAP  
//     |-----------------|
//     |                 |
//     ******************|
//     |-----------------*
//     *                 *
//     *******************
//
//     INSIDE_OVERLAP
//   **********************
//	 *                    *
//   *     ---------      *
//   *     |       |      *
//   *     |       |      *
//   *     ---------      *
//   *                    *
//   **********************   
//   
//     CROSS_OVERLAP
//          -----
//          |   |           
//          |   |
//     *****|***|******
//     *    |   |     *
//     *    |   |     *
//     *****|***|******
//          |   |  
//          -----         
typedef enum EM_RectOverlapType
{
	NO_OVERLAP,
    CORNER_OVERLAP,
	ANCHOR_OVERLAP,
	SAME_WIDTH_OVERLAP,
	INSIDE_OVERLAP,
	CROSS_OVERLAP,
	ERROR_OVERLAP,
}EM_RectOverlapType;

//判断Rect重叠的类型,判定方法
//NO_OVERLAP:     重叠矩形面积为0 
//CORNER_OVERLAP: 重叠矩形的一个角的两条边,与被判定矩形的一个角的两条边坐标一致(角的2条边)
//SAME_WIDTH_OVERLAP: 两个矩形的横向坐标或者纵向坐标一致,并且重叠。
//ANCHOR_OVERLAP: 重叠矩形的三条边与被判定矩形的三条边重叠(3个边)
//INSIDE_OVERLAP: 重叠矩形与被判定矩形的其中之一完全重合(4个边)
//CROSS_OVERLAP : 重叠矩形的平行的两条边与被判定矩形之一的对应两条边重叠,另两条边与另一个被判定矩形的对应的另两条边重叠(2对对应的两条边)
//后三种的相同部分是都有平行的两条边重合,所以判定先重后三行开始
EM_RectOverlapType JudgeOverlapTypeByTwoRect(const EM_RECT& firstRect,const EM_RECT& overlapRect)
{
	if ( (overlapRect.left == firstRect.left) && (overlapRect.right==firstRect.right))//平行2条边
	{
		if ( (overlapRect.top == firstRect.top) )//3条边
		{
			if (overlapRect.bottom == firstRect.bottom)//4条边
			{
				return INSIDE_OVERLAP;
			}
			else
			{
				return ANCHOR_OVERLAP;
			}
		}
		else if (overlapRect.bottom == firstRect.bottom)
		{
			if (overlapRect.top == firstRect.top)
			{
				return INSIDE_OVERLAP;
			}
			else
			{
				return ANCHOR_OVERLAP;
			}
		}
		else
		{
			return SAME_WIDTH_OVERLAP;
		}
	}
	else if ((overlapRect.top == firstRect.top) && (overlapRect.bottom == firstRect.bottom) )
	{
		return SAME_WIDTH_OVERLAP;
	}
	else 
	{
		return CORNER_OVERLAP;
	}
}

EM_RectOverlapType JudgeOverlapType(const EM_RECT& firstRect,const EM_RECT& secondRect,const EM_RECT& overlapRect)
{
	EM_RectOverlapType firstType = JudgeOverlapTypeByTwoRect(firstRect,overlapRect);
	if (ERROR_OVERLAP != firstType)
	{   
	    return firstType;
	}
	else
	{
		EM_RectOverlapType secondType = JudgeOverlapTypeByTwoRect(secondRect,overlapRect); 
		if (ERROR_OVERLAP != secondType)
		{
			return secondType;
		}
		else
		{
			return ERROR_OVERLAP;
		}
	}
}

EM_RectOverlapType GetRectOverlapType(const EM_RECT& firstRect,const EM_RECT& secondRect)
{
	EM_RECT overlapRect = GetOverlapRect(firstRect,secondRect);
	long overlapRectSize = GetEM_RECTSize(overlapRect);
	if (overlapRectSize)
	{
		return JudgeOverlapType(firstRect,secondRect,overlapRect);
	}
	else
	{
		return NO_OVERLAP;
	}
}

  

原文地址:https://www.cnblogs.com/Dennis-mi/p/3812072.html