Angle between points on plane representation of torus

anglegeneral-topologygeometry

Given two two-dimensional points $p_1 = (x_1, y_1)$ and $p_2 = (x_2, y_2)$ I can easily calculate the angle $\theta$ (with the x axis as basis) between the points with the following formula:
$$
\theta = atan2(y_1- y_2, x_1 -x2)
$$

I was wondering if there is a similar formula if i want two calculate the the angle on a (square) plane that is the representation of a torus. So to make an example lets say i have a square (representing a torus) with dimensions 1 x 1 and the points $p_1 = (0.5, 0.9)$ and $p_2 = (0.3, 0.1)$ as shown in this image:

My formula would give me (obviously) the angle $\beta$ but i want the angle defined by the shortest distance between the points $\alpha$.
I already found out how two get correct distance between those points (https://stackoverflow.com/a/2123977/7018093) but can't figure out the angle. Obviously the solution should work at any border of the square.

Best Answer

I have now come up myself with a solution that I am still not 100% sure that it is correct, but I have tested it for many examples and it seems to work. Also I am not sure if there isn't a more elegant/efficient solution. But in case anyone has the same problem I wanted to share my solution here:

For a given function

$$ \operatorname{\tau}(x):={\begin{cases}-1&{\text{if }}|x|>0.5,\\1&{\text{else}}\end{cases}} $$

the angel should be defined by

$$ a(x_1, y_1, x_2, y_2) = atan2(\tau(y_2-y_1) \cdot (y_2-y_1), \tau(x_2-x_1) \cdot(x_2, x_1)) $$

or in a (for me) more intuitive python code way:

def angle_computation(x1, y1, x2, y2):
    if abs(x2 - x1) < (1 - abs(x2 - x1)):
        deltax = x2 - x1
    else:
        deltax = (x2 - x1) * - 1

    if abs(y2 - y1) < (1 - abs(y2 - y1)):
        deltay = y2 - y1
    else:
        deltay = (y2 - y1) * - 1
    return math.atan2(deltay, deltax)