# Reverse this formula for calculating point around central point

The below formula calculates the X/Y coordinates around an X/Y center point, based on angle and distance. Now I need to reverse this formula to do the opposite: Calculate the angle/distance based on its X/Y coordinates.

This was written in Delphi, but I think this code speaks for itself:

function AroundPoint(Center: TPoint; Distance: Single; Degrees: Single): TPoint;
var
begin
Radians:= (Degrees - 135) * Pi / 180;
end;

• Center = X/Y coordinates of the center point.
• Distance = Distance away from the center point.
• Degrees = Angle around center point, upward being 0 degrees.
• Result = X/Y coordinates of resulting point.

Basically, I need to calculate based on these parameters instead:

• Center = X/Y coordinates of the center point.
• Position = X/Y coordinates of the current point.
• Result = Distance and Degrees of point, relative to Center.

How do I calculate the reverse of this formula? I'm not looking for Delphi code, but the formula itself.

Just some background on the reason: I'm creating a sample app in Delphi which simulates flying at warp speed through stars. The star positions are based on angle/distance from the center of the screen. Instead, I'd like to base positions on X/Y(/Z) coordinates initially, then move them relative to the center point.

The problem with creating the stars from center/angle/distance is that it becomes obvious from a visual standpoint, and most of them are focused towards the center. So rather than randomizing from the center point, I will randomize new stars in X/Y coordinates, then still move them from the center point.

• Just for your benefit, you'll get far more feedback if you use mathjax. – user335907 Jul 7 '17 at 0:57
• @james.nixon Math is my weakest subject, and I only go on this level about once every 3 or so years :-) – Jerry Dodge Jul 7 '17 at 0:58

1. You can easily compute distance $r$ using the euclidean distance: $$r=\sqrt{(x-x_0)^2+(y-y_0)^2}$$
2. To compute an angle $\varphi$:

• Notice, that for $\cos \varphi = \frac{x-x_0}{r}$.
• For $0\leq \varphi \leq \pi$ we have $\varphi = \arccos(\cos \varphi)$
• For $\pi< \varphi < 2\pi$ we have $\varphi = 2\pi - \arccos(\cos \varphi)$
• For $0\leq \varphi \leq \pi$ we have $y \geq y_0$
• For $\pi< \varphi < 2\pi$ we have $y<y_0$

So: $$\varphi (x,y) = \begin{cases}\arccos \frac{x-x_0}{r} &, y\geq y_0\\2\pi-\arccos \frac{x-x_0}{r} &, y< y_0 \end{cases}$$

To compute your angle in degrees (and moved by $135^{\circ}$): $$\theta = 135^{\circ}+ 180^{\circ}\frac{\varphi}{\pi}$$

I just wish to point out how easy this problem is to solve in the complex plane. The equation for a circle about a center point, say $z_0=x_0+iy_0$, of radius $r$ is given by

$$z=x+iy=z_0+re^{i\theta}$$

So, if you know $x,y,z_0$ and want to find $r,\theta$ then it is simply

$$r=|z-z_0| \quad\& \quad\theta=\tan^{-1}\frac{\mathfrak{Im} (z-z_0)}{\mathfrak{Re} (z-z_0)}$$

I've written complex routines in Pascal (many years ago) and you can probably write your own in Delphi or find some through a user's group or some such. Notice that in this approach you do not have to worry about which quadrant you are in (although you must use an arctangent routine that takes that into account).

• $\tan ^{-1}$ results only in $(\frac{-\pi}{2}, \frac{\pi}{2})$. For the numbers with nonpositive real part you will obtain fake result (or error, if . $\mathfrak{Re} (z-z_0) = 0$) – Jaroslaw Matlak Jul 10 '17 at 8:54
• I have no idea what you are talking about. Any decent computer language has an arctangent routine that takes $x$ and $y$ into account separately rather than just $y/x$, and can thus give the correct angle in any quadrant. – Cye Waldman Jul 10 '17 at 15:06