mirror of https://github.com/axmolengine/axmol.git
Implement Rect::intersectsCircle
This method checks the intersect status between the rect and a circle.
This commit is contained in:
parent
8a160d76a3
commit
2a0524322e
|
@ -188,6 +188,40 @@ bool Rect::intersectsRect(const Rect& rect) const
|
|||
rect.getMaxY() < getMinY());
|
||||
}
|
||||
|
||||
bool Rect::intersectsCircle(const cocos2d::Vec2 ¢er, 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();
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue