Speed optimization in avoid_crossing_perimeters