# Calculate the distance between the tops of two triangles with a shared base, where all sides are known?

What is the simplest way to calculate the distance between the 'top' point of two triangles, when they share the same base? The available information is the side lengths of both triangles, and ideally I'd avoid calculating the coordinates of the taller triangle altogether.

An example is at: https://www.desmos.com/calculator/4tu2dghalr , where I'm interested in determining the length of the orange line CD. Point D can be either inside or outside the taller triangle.

A link to that desmos picture

So far I've tried:

• Calculating the area of both triangles using Heron's formula based on the perimeters (all sides are known). From that, I can get the heights of both triangles, but no further.
• By hand/calculator I can use the cosine rule to, one-by-one, work out all the interior angles and ultimately form either triangle ACD or BCD, and solving for side DC again with the cosine rule. This is not ideal in my case because I'd like to do this programmatically and avoid having to decide which triangle to climb to get to CD, because the calculations will be vectorised over many, many D points.

As context, these two triangles sit within three circles. Two circles centred at A and B have a third circle centred at C that is touching both circles, all have known radius. I'd like to calculate whether point D is in the circle surrounding C or not, by determining whether the distance CD is smaller than the radius of circle C.

Link to hand-drawn context for the triangle problem.

Thanks!

• I think it's not going to be super pretty, but I would do it just using trigonometry. Let the base be $b$, and the other sides of the triangles be $A_1, A_2$ (first triangle) and $B_1, B_2$ (second triangle). Now immediately calculate auxiliary variables $$\eta_A = \frac{A_1^2 - A_2^2 +b^2}{2b} \qquad \eta_B = \frac{B_1^2 - B_2^2 +b^2}{2b}$$ Now the coordinates of the top point $D$ are $$D = (\eta_1 , \sqrt{A_1^2 - \eta_A ^2})$$ and, similarly, the coordinates of the second point are $$C = (\eta_2 , \sqrt{B_1^2 - \eta_B ^2})$$ from which you can calculate the distance. – Matti P. Mar 26 at 8:46

Let the two known (shared) vertices be $$A = (x_A , 0)$$ and $$B = (x_B , 0)$$, and the two unknown vertices be $$C = (x_C , y_C)$$ and $$D = (x_D , y_D)$$.

Let the known variables be $$x_A$$ and $$x_B$$, and the distances \begin{aligned} L_{AB} &= x_B - x_A \gt 0 \\ L_{AC} &= \left\lVert\overline{A C}\right\rVert \gt 0 \\ L_{BC} &= \left\lVert\overline{B C}\right\rVert \gt 0 \\ L_{AD} &= \left\lVert\overline{A D}\right\rVert \gt 0 \\ L_{BD} &= \left\lVert\overline{B D}\right\rVert \gt 0 \\ \end{aligned}

Then, the system of equations that determines the location of $$C$$ and $$D$$ is \left\lbrace \begin{aligned} L_{AC}^2 &= (x_C - x_A)^2 + y_C^2 \\ L_{BC}^2 &= (x_C - x_B)^2 + y_C^2 \\ L_{AD}^2 &= (x_D - x_A)^2 + y_D^2 \\ L_{BD}^2 &= (x_D - x_B)^2 + y_D^2 \\ \end{aligned} \right . This has four equations and four unknowns, and can be treated as two completely separate systems of equations, each with two unknowns ($$x_C$$ and $$y_C$$, and $$x_D$$ and $$y_D$$, respectively). The solution is \left\lbrace \begin{aligned} \displaystyle x_C &= \frac{ L_{AC}^2 - L_{BC}^2 + x_B^2 - x_A^2 }{2 ( x_B - x_A )} \\ \displaystyle y_C &= \pm \sqrt{ L_{AC}^2 - (x_C - x_A)^2 } = \pm \sqrt{ L_{BC}^2 - (x_C - x_B)^2 } \\ \displaystyle x_D &= \frac{ L_{AD}^2 - L_{BC}^2 + x_B^2 - x_A^2 }{2 ( x_B - x_A )} \\ \displaystyle y_D &= \pm \sqrt{ L_{AD}^2 - (x_D - x_A)^2 } = \pm \sqrt{ L_{BD}^2 - (x_D - x_B)^2 } \\ \end{aligned} \right. If the two triangles extend to the same side, then we can choose the positive signs above (since $$y_C \gt 0$$ and $$y_D \gt 0$$). Both right sides for the two $$y$$ coordinates yield the same answer.

After solving $$(x_C , y_C)$$ and $$(x_D , y_D)$$ as above, their distance is obviously $$L_{CD} = \sqrt{ (x_D - x_C)^2 + (y_D - y_C)^2 }$$ Because the distance is necessarily nonnegative, you do not actually need the distance $$L_{CD}$$ itself; you can just compare the distance squared, $$L_{CD}^2$$, to the radius squared, $$r_C^2$$, because nonnegative values compare the same way (less, equal, greater) as their squares do. Expanding the above after squaring yields \begin{aligned} L_{CD}^2 &= \left( \frac{ L_{AC}^2 + L_{BD}^2 - L_{AD}^2 - L_{BC}^2 }{ 2 (x_B - x_A) } \right)^2 + \left( \sqrt{ L_{AC}^2 - (x_C - x_A)^2 } - \sqrt{ L_{AD}^2 - (x_D - x_A)^2 } \right)^2 \\ ~ &= \left( \frac{ L_{AC}^2 + L_{BD}^2 - L_{AD}^2 - L_{BC}^2 }{ 2 (x_B - x_A) } \right)^2 + \left( \sqrt{ L_{BC}^2 - (x_C - x_B)^2 } - \sqrt{ L_{BD}^2 - (x_D - x_B)^2 } \right)^2 \\ \end{aligned} Both yield the same solution, to within numerical precision used.

Note that if you had just placed $$A = (0, 0)$$, ie. $$x_A = 0$$, you'd have gotten somewhat simpler solutions (and the math would have been easier, too).

• Thanks for the answer and nice geometry lesson! – lewiso1 Mar 26 at 22:29

Maybe you are interested in the Cayley-Menger determinant (see also here). In the case $$n=3$$ this determinant takes the $${4\choose2}=6$$ distances between $$4$$ points as input and computes the $$3$$-dimensional volume of the simplex the points determine. The determinant is $$=0$$ if the $$4$$ points are lying in a $$2$$-dimensional plane.

In your case there are $$4$$ points in the plane, and you know $$5$$ of the mutual distances. Since the Cayley-Menger determinant of the $$4$$ points is zero you obtain an equation for the last distance. Note that this distance is not uniquely determined, since, e.g., you can flip one of the triangles around the base.

• Definitely am interested - thanks a lot. Ultimately Ill compare this to the solution above for computation speed in numpy. – lewiso1 Mar 26 at 22:32