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))); }
bool FGAFRect::IntersectsCircle(const FVector2D ¢er, float radius) const { FVector2D rectangleCenter((Origin.X + Size.Width / 2), (Origin.Y + Size.Height / 2)); float w = Size.Width / 2.f; float h = Size.Height / 2.f; float dx = FMath::Abs(center.X - rectangleCenter.X); float dy = FMath::Abs(center.Y - rectangleCenter.Y); if (dx > (radius + w) || dy > (radius + h)) { return false; } FVector2D circleDistance(FMath::Abs(center.X - Origin.X - w), FMath::Abs(center.Y - Origin.Y - h)); if (circleDistance.X <= (w)) { return true; } if (circleDistance.Y <= (h)) { return true; } float cornerDistanceSq = FMath::Pow(circleDistance.X - w, 2.f) + FMath::Pow(circleDistance.Y - h, 2.f); return (cornerDistanceSq <= (FMath::Pow(radius, 2.f))); }