0
$\begingroup$

I am attempting to write a function (programming, not mathematics) which calculates the shortest distance between two points on a toroid. I got the following formula from StackOverflow, but I don't recognize the min portion. Can someone explain that to me, please?

sqrt(min(|x1 - x2|, w - |x1 - x2|)^2 + min(|y1 - y2|, h - |y1-y2|)^2)

1 Answers 1

3

As explained in the question you reference, this toroid is created by identifying the sides of a rectangle $w$ units wide in the x direction and $h$ units high in the y direction. So assume you have two points $(x_1, y)$ and $(x2, y)$, at the same height and let $x_2>x_1$. To get from $(x_2, y)$ to $(x_1, y)$ you can either go left by a distance $(x_2-x_1)$, or you can go right (off the edge) and come around a distance of $(w-x_2)+x_1$. The min just picks smaller of these. Similarly in y.

  • 0
    Thanks a lot. I read through the other answers and figured that out, but I appreciate your answer.2010-12-10