判断两矩形是否相交

 1 //判断矩形是否相交
2 bool FMath::IsRectIntersect(const FRect& rect1, const FRect& rect2)
3 {
4 bool bResult = true;
5
6 double dWidthRectA;
7 double dHeightRectA;
8
9 double dWidthRectB;
10 double dHeightRectB;
11
12 //方便计算,中心点坐标为实际坐标的2倍
13 CPoint centerRectA;
14 CPoint centerRectB;
15
16 //bottom的y坐标比top的大
17 dWidthRectA = rect1.GetBottomRight().GetX() - rect1.GetTopLeft().GetX();
18 dHeightRectA = rect1.GetBottomRight().GetY() - rect1.GetTopLeft().GetY();
19
20 dWidthRectB = fabs(rect2.GetBottomRight().GetX() - rect2.GetTopLeft().GetX());
21 dHeightRectB = fabs(rect2.GetBottomRight().GetY() - rect2.GetTopLeft().GetY()) ;
22
23 centerRectA.x = GetDoubleInt(rect1.GetTopLeft().GetX()/2 + rect1.GetBottomRight().GetX()/2);
24 centerRectA.y = GetDoubleInt(rect1.GetTopLeft().GetY()/2 + rect1.GetBottomRight().GetY()/2);
25
26 centerRectB.x = GetDoubleInt(rect2.GetTopLeft().GetX()/2 + rect2.GetBottomRight().GetX()/2);
27 centerRectB.y = GetDoubleInt(rect2.GetTopLeft().GetY()/2 + rect2.GetBottomRight().GetY()/2);
28
29
30 //判断两个矩形的中心坐标的水平和垂直距离
31 if ( (fabs(double(centerRectA.x - centerRectB.x)) <= (dWidthRectA + dWidthRectB))
32 && (fabs(double(centerRectA.y - centerRectB.y)) <= (dHeightRectA + dHeightRectB)) )
33 {
34 bResult = true;
35 }
36 else
37 {
38 bResult = false;
39 }
40
41 return bResult;
42
43 }

转载于:https://www.cnblogs.com/hbf369/archive/2011/12/09/2281554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值