In case of points very close to each other, it might occur that both points of...
In case of points very close to each other, it might occur that both points of the next curve are within the tolerance. To make sure that the right one is picked, make sure that d is smaller than d_reversed as well.