Wrap Around/Toroidal? Distance

435 Views Asked by At

I am struggling to get a distance metric that works on a plane that repeats/tiles itself similar to the way a walking on a torus works. An analogy to the Euclidean distance between 2 points but on a torus.

1

There are 1 best solutions below

0
On

Let's say the plane ($\mathbb{R}^2$) is tiled by $1\times 1$ squares, so if you have two points $(x_1,x_2)$ and $(y_1,y_2)$, their "torus distance" will be the same as the torus distance between $(x_1,x_2)$ and $(y_1+n,y_2+m)$ where $n,m$ are integers.

Let's use $\operatorname{frac}(x)$ to denote the fractional part of $x$ in the sense of the difference between $x$ and $x$ rounded down. So for example, $\operatorname{frac}(4.2)=0.2$ and $\operatorname{frac}(-4.2)=0.8$. Something we know is that the distance metric should satisfy $$d((x_1,x_2),(y_1,y_2))=d((\operatorname{frac}(x_1),\operatorname{frac}(x_2)),(\operatorname{frac}(y_1),\operatorname{frac}(y_2)))$$ since this represents shifting the two points to lie in the unit square $[0,1)\times[0,1)$. Another thing we know is that the distance metric should satisfy $$d((x_1,x_2),(y_1,y_2))=d((x_1-y_1,x_2-y_2),(0,0))$$ since translating the plane shouldn't change the metric. Putting these together, we have $$d((x_1,x_2),(y_1,y_2))=d((\operatorname{frac}(x_1-y_1),\operatorname{frac}(x_2-y_2)),(0,0)).$$ What remains is to figure out what $d((x,y),(0,0))$ is when $0\leq x<1$ and $0\leq y<1$. The nearest translates of $(0,0)$ to $(x,y)$ are $(0,0)$, $(0,1)$, $(1,0)$, and $(1,1)$, which are the four corners of the unit square. So, we have $$d((x,y),(0,0)) = \min\left\{\sqrt{x^2+y^2},\sqrt{x^2+(y-1)^2},\sqrt{(x-1)^2+y^2},\sqrt{(x-1)^2+(y-1)^2}\right\}$$ which can be simplified to $$d((x,y),(0,0)) = \sqrt{\left(\min\{x,1-x\}\right)^2 +\left(\min\{ y,1- y\}\right)^2 }.$$

Therefore, the full torus metric can be written as $$d((x_1,x_2),(y_1,y_2)) = \sqrt{\left(\min\{\operatorname{frac}(x_1-y_1), 1-\operatorname{frac}(x_1-y_1)\}\right)^2 +\left(\min\{\operatorname{frac}(x_2-y_2),1- \operatorname{frac}(x_2-y_2)\}\right)^2 }.$$