add the calNearest function