Implement Rect::intersectsCircle

This method checks the intersect status between the rect and a circle.
This commit is contained in:
giginet 2015-04-12 17:32:11 +09:00
parent 8a160d76a3
commit 2a0524322e
2 changed files with 39 additions and 0 deletions

View File

@ -188,6 +188,40 @@ bool Rect::intersectsRect(const Rect& rect) const
rect.getMaxY() < getMinY());
}
bool Rect::intersectsCircle(const cocos2d::Vec2 &center, float radius) const
{
Vec2 rectangleCenter((origin.x + size.width / 2),
(origin.y + size.height / 2));
float w = size.width / 2;
float h = size.height / 2;
float dx = fabs(center.x - rectangleCenter.x);
float dy = fabs(center.y - rectangleCenter.y);
if (dx > (radius + w) || dy > (radius + h))
{
return false;
}
Vec2 circleDistance(fabs(center.x - origin.x - w),
fabs(center.y - origin.y - h));
if (circleDistance.x <= (w))
{
return true;
}
if (circleDistance.y <= (h))
{
return true;
}
float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2);
return (cornerDistanceSq <= (powf(radius, 2)));
}
void Rect::merge(const Rect& rect)
{
float top1 = getMaxY();

View File

@ -196,6 +196,11 @@ public:
*/
bool intersectsRect(const Rect& rect) const;
/**
Check the intersect status of the rect and a circle.
* @js NA
*/
bool intersectsCircle(const Vec2& center, float radius) const;
/**
Get the min rect which can contain this and rect.
* @js NA
* @lua NA