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

anglecirclesgeometrypolygonstriangles

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!

Best Answer

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

Related Question