Fixing simulation to find shortest rope path

I’m trying to make a simulation-type thing where it simulates the shortest distance a rope can take around a polygon, and I’ve run into the error of it only calculating the closest distance once, and I don’t know how to make it calculate each time it finds a new point. Any advice?

Also when i reverse the positions of P1 and P0 it completely breaks

I think what’s happening it that its finding the shortest path to the other point, like see what happens when they are on opposite sides of the poly gon, maybe do some peice wise to say that if it isn’t done,the first line must be positive slope because that would help it from reversing.but that’s just my opinion