rewrote 1-D distance function