minDistance
) and the index of the closest valid point (closestIndex
), setting them to Infinity
and -1
respectively.minDistance
, update minDistance
and closestIndex
with the current distance and index.minDistance
, update closestIndex
only if the current index is smaller than closestIndex
.closestIndex
is still -1
, which means no valid point was found, and return -1
.closestIndex
as the index of the closest valid point.