# Distance between point and a spiral

I'm trying to work out an algorithm where, given the equation for a spiral in polar coordinates, $r(\theta)$, and a point rectilinear coordinates, $P(x,y)$, I can work out the minimum distance between that point and the curve traced by that spiral. I was hoping to have a general solution for any spiral form, but if that's not possible, then a solution for a Fermat spiral $$r(\theta)=\pm a\sqrt{\theta}$$ would be most preferable.

I've tried to find a global minima using the second and third differential of the distance equation, but I keep getting stumped. Any one have any ideas? Thanks very much in advance.

• You could convert the point into polar coordinates, $(r_P,\theta_P)$, so that the square of its distance from $(r,\theta)$ is $r^2+r_P^2-2rr_P\cos(\theta-\theta_P)$. That seems easier to differentiate as a function of $\theta$.
– user856
Commented Jul 25, 2012 at 15:38

Suppose the spiral is $r = f(\theta)$. So, any point on the spiral can be represented as $(f(\theta),\theta)$. The reference point $P(x,y)$ is fixed. For easier computation, convert P to polar coordinates as $P(r_0,\theta_0)$. Let $D(\theta)$ denote the squared distance between the point $(f(\theta),\theta)$ on the spiral and the reference point P.

$$D(\theta) = f^2(\theta)+r_0^2 - 2f(\theta)r_0\cos(\theta-\theta_0)$$ For the spiral that you have mentioned, consider the positive branch $f(\theta) = a\sqrt{\theta}$ .

$$D(\theta) = a^2\theta + r_0^2 -2 a r_0\sqrt{\theta}\cos(\theta-\theta_0)$$ $$D'(\theta) = a^2 +2ar_0\sqrt{\theta}\sin(\theta-\theta_0)-\frac{ar_0\cos(\theta-\theta_0)}{\sqrt{\theta}}$$

If $D'(\theta)=0$, $$g(\theta)=\frac{\cos(\theta-\theta_0)-2\theta\sin(\theta-\theta_0)}{\sqrt{\theta}} = \frac{a}{r_0}$$
Though no easy closed form solution presents itself, the problem is very easy to solve numerically as the LHS is just a function of $\theta$ while the RHS is a constant. You just need to keep evaluating $g(\theta)$ in a suitable interval until $g(\theta)\approx\frac{a}{r_0}$ up to the desired tolerance.

In order to find out the required interval, note that $g(\theta) \le \frac{1+2\theta}{\sqrt{\theta}}$. So, we get an upper bound on $\theta$, say $\theta^*$, for the given $a$. Search in $(0,\theta^*)$ for a value of $\theta$ that satisfies $g(\theta) =\frac{a}{r_0}$.

Similarly, you can deal with the negative branch $f(\theta) = -a\sqrt{\theta}$.

• @BarryCipra - Thanks. Corrected it. Commented Dec 23, 2013 at 12:42
• There is a closed form solution. I found it and posted it here. Commented Jul 20, 2016 at 10:09

I was the one who asked this question, all the way back in 2012. I figure I should answer it, now that I've finally figured it out after all these years.

Going by Rahul's comment, you have the spiral distance function given by: $$D^2_{\theta,T}=R_\theta^2+\left|T\right|^2-2\left|T\right|R_\theta \cos\left(\theta_T - \theta\right)$$ which is the cosine rule. This function is periodic, such that $D_{\theta,T}$ is at a minimum when $\theta_T-\theta=2\pi n \rightarrow \theta = \theta_T - 2\pi n$. Substituting this into the function above removes the cosine term, and the derivative then becomes invertible. $$D^2\left(\theta_T - 2\pi n\right)=R^2\left(\theta_T - 2\pi n\right)+\left|T\right|^2-2\left|T\right|R\left(\theta_T - 2\pi n\right)$$

$$-2\pi \frac{d}{dn} D^2\left(\theta_T - 2\pi n\right)=-4\pi R\left(\theta_T - 2\pi n\right) R^\prime\left(\theta_T - 2\pi n\right) + 4\pi\left|T\right|R^\prime\left(\theta_T - 2\pi n\right) = 0$$

$$4\pi R\left(\theta_T - 2\pi n\right) R^\prime\left(\theta_T - 2\pi n\right) = 4\pi\left|T\right|R^\prime\left(\theta_T - 2\pi n\right)$$

$$R\left(\theta_T - 2\pi n\right) = \left|T\right| \rightarrow \theta = R^-\left(\left|T\right|\right)$$

Substituting this back into the spiral distance function, we have:

$$D^2_T = R^2\left(R^-\left(\left|T\right|\right)\right)+\left|T\right|^2-2\left|T\right|R\left(R^-\left(\left|T\right|\right)\right) \cos\left(\theta_T - R^-\left(\left|T\right|\right)\right)$$ $$\rightarrow D^2_T = 2\left|T\right|^2-2\left|T\right|^2 \cos\left(\theta_T - R^-\left(\left|T\right|\right)\right) = 2\left|T\right|^2 \text{ver}\left(\theta_T - R^-\left(\left|T\right|\right)\right)$$

Therefore, the distance of any given point $T$ to a spiral curve defined by $R_\theta$ is $$D_{R,T} = \left|T\right|\sqrt{2\text{ver}\left(\theta_T - R^-\left(\left|T\right|\right)\right)}$$

Happy times!

Use a golden logarithmic spiral. The distance from any point to the origin (in either polar or rectangular coordinates) is equal to a(phi)^(theta/(pi/2)) where a is the distance from an intersection of the spiral an the x axis, and theta is the angle of rotation (of the spiral) when said point lies on its curve.

To find the minimum distance of the point to an arm of the spiral when the spiral arm is between the point and origin, simply subtract the radius value of the closest arm to the point from the distance of the point to the origin.

When the point is between the spiral arm and the origin, its minimum distance from the spiral arm is the absolute value of the radius of that part of the spiral minus the distance from the point to the origin.

Since all spirals have rotational symmetry, this method works for all spirals (Fermat's included). Using the golden spiral is simply easiest because of how "intuitive" it is and the properties of phi (makes calculations far simpler than any other spiral).

Feel free to contact me at [email protected] if you have any questions or need clarification.

• You seem to be assuming that the closest point on the spiral lies along the line from the origin to the point in question, which is not correct.
– user856
Commented Dec 17, 2013 at 19:51