//矩形和矩形相交判断 Bool HawkRect2D::Intersect(const HawkRect2D& oRect) const { Float fDis = (GetBoundCenter() - oRect.GetBoundCenter()).Length(); if (fDis > GetBoundRadius() + oRect.GetBoundRadius()) { return false; } for (Int32 i=0;i<4;i++) { HawkLine2D oLine(GetPoint(i),GetPoint((i+1)%4)); PtRelation eRel = IsClockWise()?PT_LEFT:PT_RIGHT; if (oLine.GetPtRelation(oRect.GetPoint(0)) == eRel && oLine.GetPtRelation(oRect.GetPoint(1)) == eRel && oLine.GetPtRelation(oRect.GetPoint(2)) == eRel && oLine.GetPtRelation(oRect.GetPoint(3)) == eRel) return false; } for (Int32 i=0;i<4;i++) { HawkLine2D oLine(oRect.GetPoint(i),oRect.GetPoint((i+1)%4)); PtRelation eRel = oRect.IsClockWise()?PT_LEFT:PT_RIGHT; if (oLine.GetPtRelation(GetPoint(0)) == eRel && oLine.GetPtRelation(GetPoint(1)) == eRel && oLine.GetPtRelation(GetPoint(2)) == eRel && oLine.GetPtRelation(GetPoint(3)) == eRel) return false; } return true; }
//直线和矩形相交的交点 Bool HawkRay2D::Intersect(const HawkRect2D& oRect,Vec2IntrResult* pResult) const { //包围圆与对应直线的关系 HawkVector2D vCenter = oRect.GetBoundCenter(); Float fRadius = oRect.GetBoundRadius(); if (GetDistance(vCenter) > fRadius) { return false; } PtRelation eRel[4] = {PT_NONE}; for (Int32 i=0; i<4; i++) { HawkVector2D oVec2 = oRect.GetPoint(i); eRel[i] = GetPtRelation(oVec2); if (eRel[i] == PT_ON) { if (pResult) { if(!HawkMath::IsZero(Direction.X)) { pResult->Factor = (oVec2.X-Point.X) / Direction.X; } else if(!HawkMath::IsZero(Direction.Y)) { pResult->Factor = (oVec2.Y-Point.Y) / Direction.Y; } else { T_Exception("Direction Is Zero."); } pResult->Point = oVec2; } return true; } } for (Int32 i=0; i<4; i++) { if (eRel[i] != eRel[(i+1)%4]) { HawkSegment2D oSeg(oRect.GetPoint(i),oRect.GetPoint((i+1)%4)); if(Intersect(oSeg,pResult)) return true; } } return false; }