Reverse this formula for calculating point around central point

225 Views Asked by At

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
  Radians: Real;
begin
  Radians:= (Degrees - 135) * Pi / 180;
  Result.X:= (Distance*Cos(Radians)-Distance*Sin(Radians))+Center.X;
  Result.Y:= (Distance*Sin(Radians)+Distance*Cos(Radians))+Center.Y;
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.

2

There are 2 best solutions below

0
On
  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}$$

2
On

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).