fix nearest using perpendicular distance to rectangle