public static float SquaredDistance(this FloatRect f, Vector2f v) { var max = f.Max(); var dx1 = v.X - max.X; if (dx1 < 0) { dx1 = 0f; } var dx = f.Left - v.X; if (dx1 > dx) { dx = dx1; } var dy1 = v.Y - max.Y; if (dy1 < 0) { dy1 = 0f; } var dy = f.Top - v.Y; if (dy1 > dy) { dy = dy1; } return((dx * dx) + (dy * dy)); }