Anuj, Each curved segment has a large number of neighboring grid points. each of which has a unique nearest projection on the curve. The curve is naturally sampled by this projection points a very large number of times and quite uniformly. Therefore, why not divide the curve into a large number of segments to begin with and then just find whatever point is close to each grid? It could be a lot faster to find the distance this way.
I am deeply sorry if this mathematical fact offends you in any way. Your code is good but please allow me to suggest an alternative, perhaps, faster implementation. Alexei