# Quickest definition of the distance on the circle

I'm trying to implement a distance on the circle on a computer and I can't come up with an optimized way of doing it.

What I mean is that I have numbers in $[0,2\pi]$ but I'm looking at them as a segment enrolled three times on the unit circle. So, $\frac{\pi}{6}$ and $\frac{11\pi}{6}$ are only $\frac{\pi}{3}$ far from each other. What I've been doing is translating one of the two numbers left and right by two pi, and taking the minimum distance. Thus my distance function looks like:

$$dist(\theta_1,\theta_2) = \text{min}(|\theta_1-\theta_2|,|\theta_1-\theta_2+2\pi|,|\theta_1-\theta_2-2\pi|)$$

But I have the feeling that there is an smarted way of doing it. Any suggestions?

• $\min \{ \lvert \theta_1 - \theta_2\rvert,\, 2\pi - \lvert \theta_1 - \theta_2\rvert\}$ Sep 11, 2014 at 19:08
• How do you mean 'a segment enrolled three times on the unit circle'? If I look at the picture with $2$ points on the circle, I see only two possible arcs (segments). Sep 11, 2014 at 19:08