Of all objects matching a condition, find the one closest to a position. Supposedly a very common problem. The current code looks like this:
protected Foo FindClosestFooMatching (Vec pos, Func<Foo, bool> matches)
{
float bestDist = float.MaxValue;
Foo bestFoo = null;
foreach (Foo foo in AllFoos()) {
if (matches (foo)) {
float dist = pos.Dist (foo.Center);
if (dist <= bestDist) {
bestDist = dist;
bestFoo = foo;
}
}
}
return bestFoo;
}
I'm thinking of several ways to refactor this code, but failed to find a really nice one yet. If you've got a minute, give it a shot. :-)
Edit: Regarding Eric's questions. It's a standard 3D space with Euclidian metric (= fast). Point clustering and junk query likelihood is unknown.