I have a 3D triangle ABC with known Lengths AB, BC, and AC and known points A(x,y,z), B(x,y,z) and C(z). I need to be able to find the x and y coordinates of C using a non-iterative method.
I've been going through another post and was following along until the aa bb and c.z calulations in the answer. Unfortunately, I don't have enough reputation to comment on that post...
3d geometry: triangle 2 points known, find 3rd point
Any help is greatly appreciated!
The point $C$ lies somewhere on the intersection of a pair of spheres with centers $A$ and $B$. Since we also know that $C$ lies somewhere on the plane $z=z_C$, this reduces to finding the intersection of a pair of circles.
Let $r_A=|AC|$ and $r_B=|BC|$. The equations of the two spheres are $$(x-x_A)^2+(y-y_A)^2+(z-z_A)^2=r_A^2 \\ (x-x_B)^2+(y-y_B)^2+(z-z_B)^2=r_B^2.$$ If $|z_C-z_A|\gt r_A$ or $|z_C-z_B|\gt r_B$, then there’s no solution. Otherwise substitute $z=z_C$ into the above equations to get the equations of two circles: $$(x-x_A)^2+(y-y_A)^2=r_A^2-(z_C-z_A)^2 \\ (x-x_B)^2+(y-y_B)^2=r_B^2-(z_C-z_B)^2.$$ If you subtract one of these equations from the other you get a linear equation in $x$ and $y$, which you can solve for one or the other and then back-substitute into either circle equation to get a solution. The most common case will be two solutions, but if the vertices are lined up just right you might get one or an infinite number of them (the two circles coincide).
Alternatively, a direct calculation with vectors produces the points of intersection of the two circles. Let $r_A'^2=r_A^2-(z_C-z_A)^2$ and $r_B'^2=r_B^2-(z_C-z_B)^2$ be the radii of the two circles, and $d=\sqrt{(x_A-x_B)^2+(y_A-y_B)^2}$ the distance between their centers. If $r_A'+r_B'>d$, the circles clearly don’t intersect. If $d=0$, the circles are concentric, for which see below. Otherwise, the intersections of the circles are equally spaced on a line perpendicular to the line through the two centers.
Referring to the illustration above, by the Pythagorean theorem the distance of this line from the center of circle $A$ satisfies $$r_A'^2-w^2=r_B'^2-(d-w)^2$$ from which $$w={d^2+r_A'^2-r_B'^2\over2d}.$$ Again by the Pythagorean theorem, the distance $h$ along the perpendicular line to the intersection points is $h=\sqrt{r_A'^2-w^2}=\sqrt{r_B'^2-(d-w)^2}$. Taking unit vectors in the directions of the two lines, this gives for the $x$- and $y$- coordinates of the intersection points $$\frac wd(x_B-x_A,y_B-y_A)\pm\frac hd(y_A-y_B,x_B-x_A).\tag{*}$$ If $d=0$, then the $x$- and $y$- coordinates of points $A$ and $B$ are equal, so there’s one solution—$(x_A,y_A,z_C)$—when $r_A'=r_B'=0$ (the two spheres are tangent) or an infinite number when $r_A'=r_B'\gt0$. When $r_A'\ne r_B'$, there is, of course, no solution.
As an implementation note, observe that $\frac wd={d^2+r_A'^2-r_B'^2\over d^2}$ and similarly $h/d$ only involves $d^2$ after factoring in the contribution of $w^2$. All of the radii are squared in the actual computations as well, so in practice the only point at which you really need to take a square root is when computing $h$. Range checks can be delayed to avoid taking square roots: If $z_C$ is out of range, one or both of $r_A'^2$ or $r_B'^2$ will end up being negative; if $r_A'+r_B'\gt d$, then $h$ will end up being imaginary.