static void FindClosest(QuadTreeNode *n, float2 p, float2 min, float2 max, ref float dist, ref Vertex *closest) { if (n->IsLeaf) { for (int i = 0; i < n->Count; i++) { var v = n->Data[i]; var d = math.lengthsq(v->Point - p); if (d < dist) { closest = v; dist = d; } } return; } if (Math.RectsOverlap(min, max, n->Origin - n->HalfSize, n->Origin)) { FindClosest(n->BL, p, min, max, ref dist, ref closest); } if (Math.RectsOverlap(min, max, n->Origin - new float2(n->HalfSize, 0), n->Origin + new float2(0, n->HalfSize))) { FindClosest(n->TL, p, min, max, ref dist, ref closest); } if (Math.RectsOverlap(min, max, n->Origin - new float2(0, n->HalfSize), n->Origin + new float2(n->HalfSize, 0))) { FindClosest(n->BR, p, min, max, ref dist, ref closest); } if (Math.RectsOverlap(min, max, n->Origin, n->Origin + n->HalfSize)) { FindClosest(n->TR, p, min, max, ref dist, ref closest); } }