1
$\begingroup$

I'm trying to understand how this works, and mathematically I'm having difficulty.

Given 2 angles between $(-2\pi, 2\pi)$: $\theta$ and $\phi$ I want to interpolate between them by the ratio: r.

My problem is this, when interpolating between say 0 and $15\pi/8$ I don't want to get $15\pi/16$ I want to get $31\pi/16$ or $-\pi/16$.

Some math for this has been presented here: https://stackoverflow.com/a/14498790/2642059 But I can't prove to myself that for any valid $\theta$-$\phi$ combination this is workable. Can someone help me? The equation is:

$$\phi + ((((\theta-\phi)\%2\pi+3\pi)\%2\pi)-\pi)r$$

  • 1
    I don't know exactly how you want the formula to behave. Nevertheless, wolfram Alpha plots functions reasonably well. Try https://www.wolframalpha.com/input/?i=(((((15pi%2F8-x)mod2pi)%2B3pi)mod2pi)-pi)*.5 You can fix $\theta$ or $\phi$ and check the resulting one-variable function.2017-02-14

2 Answers 2

2

From origine 0 to angle

If $0\le\theta\le\pi$ then interpolation from $0\to\theta$ is $r\theta$.

If $-\pi\le\theta\le0$ then interpolation from $0\to\theta$ is also $r\theta$ (this is your short path requirement).

If $\pi\le\theta\le2\pi$ then $-\pi\le(\theta-2\pi)\le0$ and interpolation from $0\to\theta$ is then $r(\theta-2\pi)$.

If $-2\pi\le\theta\le-\pi$ then $0\le(\theta+2\pi)\le\pi$ and interpolation from $0\to\theta$ is then $r(\theta+2\pi)$.

Between two angles

This is very similar, just shift the origin by $\theta_0$ and use $\theta=\theta_1-\theta_0$

If $\theta_0\le\theta_1\le\theta_0+\pi$ then interpolation $\theta_0+r(\theta_1-\theta_0)$.

If $\theta_0-\pi\le\theta_1\le\theta_0$ then interpolation is $\theta_0+r(\theta_1-\theta_0)$.

If $\theta_0+\pi\le\theta_1\le\theta_0+2\pi$ then interpolation is $\theta_0+r(\theta_1-\theta_0-2\pi)$.

If $\theta_0-2\pi\le\theta_1\le\theta_0-\pi$ then interpolation is $\theta_0+r(\theta_1-\theta_0+2\pi)$.

Conclusion

Now if we want to be in the cases exposed before we need to have $\theta_0\in[-\pi,\pi]$ and $\theta_1\in[-\pi,\pi]$ so that the difference $(\theta_1-\theta_0)\in[-2\pi,2\pi]$

So we do :

$\theta_0:=(\theta_0\mod 2\pi)$ then if $(\theta_0>\pi)$ then $\theta_0:=\theta_0-2\pi$

$\theta_1:=(\theta_1\mod 2\pi)$ then if $(\theta_1>\pi)$ then $\theta_1:=\theta_1-2\pi$

$\varepsilon=sgn(\theta_1-\theta_0)$ [sign function, -1 if <0 and +1 if >0 else 0]

Our angles are now correct and we can interpolate :

If $|\theta_1-\theta_0|\le\pi$ then $\theta(r)=\theta_0+r(\theta_1-\theta_0)$

If $|\theta_1-\theta_0|\ge\pi$ then $\theta(r)=\theta_0+r(\theta_1-\theta_0-2\varepsilon\pi)$

Note that for $(\theta_1-\theta_0)=\pi$ it's up to you to chose the side, your short path requirement introduce a discontinuity there.


Now let's have a look at the formula your proposed :

$x=[(\theta_1-\theta_0) \mod 2\pi]$ and $0\le x\lt 2\pi$

We know that $a\mod b=a-b\lfloor\frac ab\rfloor$

In the case $0\le x\lt \pi$ then $1<\frac 32\le \frac{x+3\pi}{2\pi}<2$

$[(x+3\pi)\mod 2\pi]-\pi=(x+3\pi)-2\pi\lfloor\frac{x+3\pi}{2\pi}\rfloor-\pi=(x+3\pi)-2\pi*1-\pi=x$

And we get back our formula $\theta(r)=\theta_0+rx$

In the case $\pi\le x< 2\pi$ then $2\le \frac{x+3\pi}{2\pi}<\frac 52<3$

$[(x+3\pi)\mod 2\pi]-\pi=(x+3\pi)-2\pi\lfloor\frac{x+3\pi}{2\pi}\rfloor-\pi=(x+3\pi)-2\pi*2-\pi=(x-2\pi)$

And we get back our formula $\theta(r)=\theta_0+r(x-2\pi)$

This is quite a clever trick :-)

  • 1
    So if we let $\theta_1-\theta_0=x$ then you just proved that $$\theta(r) = \begin{cases}\theta_0 + r(x) & \text{if $x$ is $\leq \pi$}\\\theta_0 + r(x - 2\pi\frac{x}{|x|}) & \text{if $x$ is $> \pi$}\end{cases}$$ This does *do* what I want, but it adds an `if` and it doesn't prove: $$((((\theta-\phi)\%2\pi+3\pi)\%2\pi)-\pi)*r$$2017-02-14
  • 1
    I think that you meant to do "\theta_1" in your first equation after the rule.2017-02-14
  • 0
    Can you provide a little more context on how you made it to: $1<\frac 32\le \frac{x+3\pi}{2\pi}<2$ I just can't make the mental leap.2017-02-14
  • 1
    02017-02-14
  • 0
    So this is far more eloquent than my proof and i really like it for positive *x* but don't we need to prove it for *x<0* as well?2017-02-14
  • 0
    It is the same, it will be $x+3\pi+2\pi*0-\pi=x+2\pi$ and we would have the last formula. But the first modulo took care of that, so it is not useful.2017-02-14
1

You want an interpolation the shortest distance between $\theta$ and $\phi$, which means that we don't care about r.

What you're really asking is for a proof that: $-\pi\leq(((\theta-\phi)\%2\pi+3\pi)\%2\pi)-\pi\leq\pi$

We can simplify that to: $0\leq((\theta-\phi)\%2\pi+3\pi)\%2\pi\leq2\pi$

This is guaranteed to be less than $2\pi$ because of the modulo operator, so we now just need to know that: $0\leq(\theta-\phi)\%2\pi+3\pi$

Which can again be simplified to: $-3\pi\leq(\theta-\phi)\%2\pi$

This is guaranteed to be true independent of the value of $\theta-\phi$ because: $\forall x:-2\pi<(x)\%2\pi$

This proves that the angle will always be the shortest angle between $\theta$ and $\phi$.

It also needs to be true that this is the angle such that: $(((\theta-\phi)\%2\pi+3\pi)\%2\pi)-\pi=\theta-\phi$

Which simplifies to: $((\theta-\phi)\%2\pi+3\pi)\%2\pi=\theta-\phi+\pi$

I'm going to make the basic assertion that: $\forall x\in\Bbb Z^+:\psi=\psi+2x\pi$ And by extension taking the $2\pi$ modulo of any angle will simply result in the same angle.

Therefore: $(\theta-\phi)\%2\pi+3\pi=\theta-\phi+\pi$

Which simplifies to: $(\theta-\phi)\%2\pi=\theta-\phi-2\pi$

By applying my assertion to both sides we get: $\theta-\phi=\theta-\phi$