I would like to calculate the distance between two x/y coordinates on the surface of a torus. So, this is a normal grid that has the property that its corners and sides are \'co
So you are looking for the Euclidean distance on the two-dimensional surface of a torus, I gather.
sqrt(min(|x1 - x2|, w - |x1 - x2|)^2 + min(|y1 - y2|, h - |y1 - y2|)^2)
where w and h are the width (x) and height (y) of the grid, respectively.
w
h