public static float Distance(this Rectangle r, Vector2 point) { if (Utils.FloatIntersect((float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height, point.X, point.Y, 0f, 0f)) { return(0f); } if (point.X >= (float)r.Left && point.X <= (float)r.Right) { if (point.Y < (float)r.Top) { return((float)r.Top - point.Y); } return(point.Y - (float)r.Bottom); } if (point.Y >= (float)r.Top && point.Y <= (float)r.Bottom) { if (point.X < (float)r.Left) { return((float)r.Left - point.X); } return(point.X - (float)r.Right); } if (point.X < (float)r.Left) { if (point.Y < (float)r.Top) { return(Vector2.Distance(point, r.TopLeft())); } return(Vector2.Distance(point, r.BottomLeft())); } if (point.Y < (float)r.Top) { return(Vector2.Distance(point, r.TopRight())); } return(Vector2.Distance(point, r.BottomRight())); }