LPRECT lprcDst, // address of structure for intersection
CONST RECT *lprcSrc1, // address of structure with first rectangle
CONST RECT *lprcSrc2 // address of structure with second rectangle
);
用API
The IntersectRect function calculates the intersection of two source rectangles and places the coordinates of the intersection rectangle into the destination rectangle. If the source rectangles do not intersect, an empty rectangle (in which all coordinates are set to zero) is placed into the destination rectangle.
BOOL IntersectRect(
LPRECT lprcDst, // address of structure for intersection
CONST RECT *lprcSrc1, // address of structure with first rectangle
CONST RECT *lprcSrc2 // address of structure with second rectangle
);
Parameters
lprcDst
Points to the RECT structure that is to receive the intersection of the rectangles pointed to by the lprcSrc1 and lprcSrc2 parameters.
lprcSrc1
Points to the RECT structure that contains the first source rectangle.
lprcSrc2
Points to the RECT structure that contains the second source rectangle.
Return Values
If the rectangles intersect, the return value is nonzero.
If the rectangles do not intersect, the return value is zero. To get extended error information, call GetLastError.
楼主的要求是判断两个矩形是否有交点,至于有几个不用计算,那么根据两个矩形相交的特征,每个矩形至少有一条对角线和另一个矩形相交。
现建立一个数学模型:矩形A的四条边A1,A2,A3,A4,矩形B的四条边B1,B2,B3,B4,求A的两条对角线A13,A24,依次判断各条对角线两端点是否在B的范围内,B的范围:TOP,BOTTON,LEFT,RIGHT,其中一条对角线的端点PT1(X1,Y1),PT2(X2,Y2),如果LEFT<=X1<=RIGHT AND BOTTON<=Y1<=TOP并且X2不在该范围内,则相交。
这样就把比较的次数减少,同时也不需要判断两个矩形摆放位置之间的关系。