intersectsCircle method
Returns true if this rectangle intersects with a circle defined by its center
(pointX, pointY) and radius.
Implementation
bool intersectsCircle(double pointX, double pointY, double radius) {
double halfWidth = getWidth() / 2;
double halfHeight = getHeight() / 2;
double centerDistanceX = (pointX - getCenterX()).abs();
double centerDistanceY = (pointY - getCenterY()).abs();
// is the circle is far enough away from the rectangle?
if (centerDistanceX > halfWidth + radius) {
return false;
} else if (centerDistanceY > halfHeight + radius) {
return false;
}
// is the circle close enough to the rectangle?
if (centerDistanceX <= halfWidth) {
return true;
} else if (centerDistanceY <= halfHeight) {
return true;
}
double cornerDistanceX = centerDistanceX - halfWidth;
double cornerDistanceY = centerDistanceY - halfHeight;
return cornerDistanceX * cornerDistanceX + cornerDistanceY * cornerDistanceY <= radius * radius;
}