C++ 判断点否在矩形内部,矩形与矩形是否相交

1、判断点是否在矩形内部

bool pointInRect(Point2f pt, Rect rect)
{
	if ((pt.x > rect.x) && (pt.y > rect.y) && (pt.x < (rect.x + rect.width)) && (pt.y < (rect.y + rect.height)))
	{
		return true;
	}
	return false;
}

2、判断两个矩形是否相交,并且判断一个矩形是否在另一个矩形内部

bool RectInRect(const Rect& out, const Rect& in)
{
	bool bRet = true;

	int x1 = out.x;
	int y1 = out.y;
	int x2 = out.x + out.width;
	int y2 = out.y + out.height;

	int x3 = in.x;
	int y3 = in.y;
	int x4 = in.x + in.width;
	int y4 = in.y + in.height;

	bRet = (((x1 >= x3 && x1 < x4) || (x3 >= x1 && x3 <= x2)) &&
		((y1 >= y3 && y1 < y4) || (y3 >= y1 && y3 <= y2))) ? false : true;

	if (bRet == false)	 // 表示相交,相交需要判断一个矩形是否完全在另一个矩形内
	{
		if (in.x > out.x && in.y > out.y &&
			in.x + in.width < out.x + out.width &&
			in.y + in.height < out.x + out.height)
		{
			return true;  // 表示在里面
		}
		else
			return false;
	}  	
}

你可能感兴趣的:(C++,c++)