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?

  • 5
    $\begingroup$ $\min \{ \lvert \theta_1 - \theta_2\rvert,\, 2\pi - \lvert \theta_1 - \theta_2\rvert\}$ $\endgroup$ Sep 11, 2014 at 19:08
  • 1
    $\begingroup$ 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). $\endgroup$
    – Berci
    Sep 11, 2014 at 19:08


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.