Wednesday, May 08, 2013

圆与矩形碰撞检测之平方根优化版


public boolean overlapCircleRectangle(Circle c, Rectangle r) {
float closestX = c.center.x;
float closestY = c.center.y;
if(c.center.x < r.lowerLeft.x) {
closestX = r.lowerLeft.x;
}
else if(c.center.x > r.lowerLeft.x + r.width) {
closestX = r.lowerLeft.x + r.width;
}
if(c.center.y < r.lowerLeft.y) {
closestY = r.lowerLeft.y;
}
else if(c.center.y > r.lowerLeft.y + r.height) {
closestY = r.lowerLeft.y + r.height;
}
return c.center.distSquared(closestX, closestY) < c.radius * c.radius;
}

No comments:

Search This Blog

Followers