Get polar coordinates from origin

geometrypolar coordinatestransformation

In a polar coordinates system I have two point $P_1$ and $P_2$. For $P_1$ I know it coordinates from the origin ($O$) which are $(d_1,\alpha_1)$.

For $P_2$ I also know it's coordinates, $(d_2,\alpha_2)$, but it's from $P_1$. I want the coordinate of $P_2$ from the origin, $(D,A)$.

schem

I've found some post about translation in polar coordinates.

The first one gives those equations:

$$d_2^2 = d_1^2 + D^2 + 2d_1D \cos(A-\alpha_1)~~~~~~~~~~~~~~~~(1)$$

and

$$\sin(\alpha_2) = \frac{D}{d_2} \sin(A-\alpha_1)~~~~~~~~~~~~~~~~(2)$$

The second one gives those equations (I guessed some parts because he gave it with value instead of variables names):

$$(1)$$

and

$$ \tan(\alpha_2) = \frac{D \sin(A-\alpha_1)}{D \cos(A-\alpha_1)-d_1}~~~~~~~~~~~~~~~~(3)$$

My problem is, from all those equations, I don't know how to get $D$ and $A$.

EDIT:
I have to do it several times, so I'd like a solution with a small computational cost (lower than use the cartesian coordinates)

Best Answer

In the linked questions, the distances and directions from the origin of two points are known and we want the distance and direction between them. That is, in terms of your figure, $d_1, \alpha_1, D,$ and $A$ are known while $d_2$ and $\alpha_2$ are unknown.

Your problem is set differently because you already know the distance and direction between the two points that are not the origin. What you are missing is the distance and direction from the origin to one of those points.

But by understanding a little bit about why the other solutions work, you can reapply them to your different problem.

The Law of Cosines relates the length of one side of a triangle (on one side of the equation) to the angle opposite that side and to the lengths of the other two sides (on the other side of the equation).

The Law of Sines relates the ratio of a side and the angle opposite it to the ratio of another side and the angle opposite it. This is a multipurpose formula, because if you know two ratios are equal, $p/q = u/v,$ and you know any three of the quantities in the ratios, you can easily solve for the fourth quantity.

The Law of Cosines in general can be written $$ c^2 = a^2 + b^2 - 2ab \cos \theta, $$ where $a,$ $b,$ and $c$ are lengths of sides of a triangle and $\theta$ is the interior angle opposite $c.$

Since $D$ is your unknown side, you want to put $D$ for $c.$ Therefore you put $d_1$ and $d_2$ for $a$ and $b.$ Now the trickiest part of the application is that the Law of Cosines deals with the interior angle opposite the unknown side, but you have the exterior angle $\alpha_2.$ So just be aware that if $\theta$ is the interior angle, then $\theta = \pi - \alpha_2$ and $\cos\theta = - \cos\alpha_2.$ So when you make all these substitutions into the Law of Cosines you get $$ D^2 = d_1^2 + d_2^2 + 2 d_1 d_2 \cos \alpha_2. $$

Since $D$ is positive (assuming you want the usual polar coordinates, with positive radius), you can take the square root to solve for it without using a $\pm$ sign: $$ D = \sqrt{d_1^2 + d_2^2 + 2 d_1 d_2 \cos \alpha_2}. $$

This works in every case, even if $\alpha_2$ obtuse or if the direction of the angle is counterclockwise instead of clockwise (including the case where you measure $\alpha_2$ in such a way that it is greater than $180$ degrees).

The Law of Sines says $$ \frac{\sin P}{p} = \frac{\sin Q}{q} $$ where $P$ and $Q$ are angles of a triangle and $p$ and $q$ are the lengths of the sides opposite $P$ and $Q$ respectively. Note that the sine of an exterior angle is equal to the sine of the interior angle, so you can use interior or exterior angles interchangeably in this formula. Therefore you can write

$$ \frac{\sin (A - \alpha_1)}{d_2} = \frac{\sin \alpha_2}{D}. $$

Multiply both sides by $d_2,$ take the inverse sine of both sides, and add $\alpha_1$, and the result is

$$ A = \alpha_1 + \arcsin\left(\frac{d_2\sin \alpha_2}{D}\right). $$

Now you have only one thing to watch out for, which is that this solution for $A$ assumed that the angle $A - \alpha_1$ was acute. That is a drawback to the first solution you found. You can use Law of Cosines on $A - \alpha_1$ to disambiguate whether it is acute or obtuse, but now you have additional calculations to perform.

The second solution is better but can be improved further for use in a program by using the $\mathrm{atan2}$ function. The idea is that you construct a right triangle and solve for the angle; in your case you can take $d_1 + d_2 \cos\alpha_2$ as one side of the triangle and $d_2\sin\alpha_2$ as the other side. Then $$ A = \alpha_1 + \mathrm{atan2}(d_2\sin\alpha_2, d_1 + d_2 \cos\alpha_2). $$

This works even if $\alpha_2$ is obtuse or greater than $180$ degrees, as long as you measure all the angles $\alpha_1,$ $\alpha_2,$ and $A$ in the same direction, for example clockwise as in your figure.

None of these formulas particularly cares which quadrant of the plane any of the points are in or which points are "above" which other points.