There already was a question (Distance between point and sine wave), but this seems not contain the answer for me.
The formula written there is depends on x
x=e−bc⋅cos(cx+d)(a+b⋅sin(cx+d)−f)
which not provides an answer (just asks a new one question).
My definition: We have a point (x, y) and a sine function defined by (amplitude, frequency, phase). We need to find distance from point to nearest point on sine.
This needs for Signed Distance Field representation of bodies (I want to create helical springs and threads)
playground is here https://www.shadertoy.com/view/Md3yRH
Let the sinusoid be
$$y=a\sin x$$ and the point $(u,v)$.
I have omitted the phase paremeter because by translation it can be absorbed in $u$. And I have omitted the frequency parameter, because it can be absorbed in $u,v,a$ by scaling.
Now the squared distance is
$$(x-u)^2+(y-v)^2=(x-u)^2+(a\sin x-v)^2$$ and you can obtain the extrema by canceling the derivative
$$x-u+a\cos x(a\sin x-v)=0.$$
This is a nasty nonlinear equation that you have to solve by numerical methods. Then direct minimization is also possible. But the shape of the curve is a little scary.
Notice the inequality
$$(x-u)^2+(|a|-|v|)^2\le(x-u)^2+(a\sin x-v)^2\le (x-u)^2+(|a|+|v|)^2.$$
Considering the minimum of the upper bound, you get an $x$ interval where the searched minimum is guranteed to lie by solving
$$(x-u)^2+(|a|-|v|)^2=(|a|+|v|)^2.$$
From the second derivative,
$$1+a\sin x(a\sin x-v)+a^2\cos^2x=0.$$
This is a quadratic equation in $\sin x$, which you can solve analytically. It will give you the locations of the extrema of the first derivative, hence it separates the roots. By detecting the intervals with a change of sign, you can apply regula falsi.
Combining with the previous criterion, you can be sure to find the minimum with a finite number of attempts.