Calculate 3rd point of a triangle, given 2 points and all angles in 2D

2.5k Views Asked by At

I have stumbled upon an interesting problem. I tried to find an answer here but there are just too many similar threads which did not really help me, so I was trying to figure it out by myself. The task was to create a formula (because it was a programming task) for finding out coordinates of the third vertex (C) in a triangle as well as its distance from the opposite side (AB), given two other vertices as well as all the angles. It was adapted from this wiki page. Finding out the distance was not a problem, however, it was hard to find coordinates of the third point. Following is my method. I would be glad if you could check it and maybe suggest a better solution.

The first step was finding out coordinates of a point on AB, which lies on perpendicular from the missing vertex. $$S[S_x,S_y] \in AB \bigcap h\\ |h| = |AB|\frac{sin (\alpha) sin (\beta)}{sin (\alpha + \beta)} $$ Coordinates for all the points (therefore for S as well) on AB (where $\lambda$ is a scalar) : $$S=[A_x+\lambda(B_x-A_x); A_y+\lambda(B_y-A_y)]$$

Since we know that $|AS| = \frac{h}{tan(\alpha)}$, after substituing we find out that $$\lambda=\frac{h}{tan(\alpha)\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}}$$ or $\lambda=\frac{cot(\alpha)*h}{\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}}$ if you wish.

The second step was defining the perpendicular. We know the coordinates of one point which lies on it (S) as well as it's direction (normal vector to vector AB, specifically $n = (A_y - B_y; B_x - A_x)$). Thus, all the points on this line have coordinates $$C=[S_x + \kappa(A_y-B_y); S_y + \kappa(B_x-A_x)]$$ where $\kappa$ is again a scalar.

We also know that $|SC|=h$, so we get this equality $$ \sqrt{(C_x - S_x)^2 + (C_y-S_y)^2} = h $$ which after solving gives $$ \kappa = \frac{h}{\sqrt{(A_y-B_y)^2+(B_x-A_x)^2}} $$

Now it is just a matter of putting it all together. Returning to our definition of C, we know all the variables.

$$ C_x=S_x + \kappa(A_y-B_y)\\ \Downarrow\\ C_x=A_x+\lambda(B_x-A_x) + \kappa(A_y-B_y)\\ \Downarrow\\ C_x=A_x+\frac{cot(\alpha)(B_x-A_x)h}{\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}} + \frac{h(A_y-B_y)}{\sqrt{(A_y-B_y)^2+(B_x-A_x)^2}} $$

And analogically for $C_y$

$$ C_y=A_y+\frac{cot(\alpha)(B_y-A_y)h}{\sqrt{(B_x-A_x)^2+(B_y-A_y)^2}} + \frac{h(B_x-A_x)}{\sqrt{(A_y-B_y)^2+(B_x-A_x)^2}} $$

And these two lines are for the computer to solve. My question is: could it be done in a better way? I suppose it is correct, the code works well. But I am personally very interested in different ways how to solve this. Thank you.

1

There are 1 best solutions below

1
On

You have to make atleast 2 conditions: 1. When $Ax>Bx$ 2. When $Ax < Bx$

Even then it fails on this particual coordinates: $$464(Ax) \ 741(Ay)\ 22(alfa) \ 237(Bx) \ 589(By)\ 85(Beta).$$

It fails when $$Ay > By\ Ax > Bx \ but \ alfa < beta.$$ enter image description here