How to perform a linear interpolation with modulo n between two points that doesn't alter it's direction?

344 Views Asked by At

I'm trying to interpolate between the two angles $\theta_i$ and $\theta_f$ with a parameter $t$ without altering the winding of the circle which is described parametrically as follows: $$0\leq t\leq 1, \\ x=\cos(f(\theta_i,\theta_f,2\pi,t)), \\ y=\sin(f(\theta_i,\theta_f,2\pi,t))$$ where the function $f(a,b,n,t)$ describes a linear interpolation of $a$ and $b$ with a modulo $n$. The following is the function that I came up with, and it does not alter the direction of the line (or winding of the curve in my case), but it overlaps when the distance between $a$ and $b$ is greater than $n$. $$f(a,b,t)\equiv(1-t)a+tb\pmod{n}$$

My intention is that when the overlapping occurs, or in other words, when the points $a$ and $b$ cross past each other after being modulated, the line would reset by going through the shortest path but always following the same direction a non-modulated line would.

The following graph illustrates the linear function for the case when $b>a$

enter image description here

Where $\bar{a}_n\equiv a \pmod{n}$ and $\bar{b}_n\equiv b \pmod{n}$. The red line is the graph traced by the linear interpolation defined above. In order to make the overlapping more apparent, I added a vertical gap for each wind the line would make around $n$. My goal is to find a function that would define only the last wind. Lastly, the purple dotted line is the path a regular interpolation from $\bar{a}_n$ to $\bar{b}_n$ would follow. Notice that such line would go against the direction the line from $a$ to $b$ would follow.

Side note: I realized that $\mathbb{Z}$ refers to the integers not the real numbers. I don't know if it's even correct to define $\mathbb{R}$ to the modulo of $n$.

1

There are 1 best solutions below

0
On

I was able to find a function that works by using a piecewise function. It's really messy. I'm not sure if there is even a solution that would not involve piecewise functions. If there is one, I'll be glad to know it. So, here is how I solved it:

Lets define a regular linear interpolation between points $p$ and $q$ $$f(p,q,s)=(1-s)p+sq$$ Then $$\newcommand{\sgn}[1]{\operatorname{sgn}(#1)} g(a,b,n,t)= \begin{cases} f(a\mod n,b\mod n,t) & \text{if $\sgn{b-a}=\sgn{(b\mod n)-(a\mod n)} $} \\ f(a\mod n,b\mod -n,t)\mod n & \text{if $(b\mod n)>(a\mod n)$} \\ f(a\mod -n,b\mod n,t)\mod n & \text{otherwise} \\ \end{cases} $$

The line between the remainders $\bar{a}_n$ and $\bar{b}_n$ from the picture in my original question draw the line in the same direction as $a$ and $b$ if and only if the sign of $(b-a)$ is equal to the sign of $(\bar{b}_n-\bar{a}_n)$. However, in the cases when their signs do not match because they have opposite directions, I check which remainders is bigger and substitute the biggest remainder by the original variable to the negative modulo (its congruent negative pair?).