1
$\begingroup$

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.

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

$\endgroup$
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ $\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$) $\endgroup$ – Jaroslaw Matlak Jul 10 '17 at 8:54
  • $\begingroup$ 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. $\endgroup$ – Cye Waldman Jul 10 '17 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.