Diagonal of triangular bipyramid with 3 edges next to a point length 1 and orthogonal, and the lengths of three known.

80 Views Asked by At

I am working on a lighting system for a voxel game. It requires recursive euclidean distance calculation for successively further blocks, and the distance of each block from the light source needs to be calculated knowing only the distance of one previous block (if it's on the same orthogonal line as the light source), two previous blocks (if it's on the same orthogonal plane as the light source), or three previous blocks (if it's neither of those).

The 1D case is easy: just add one to the distance.

The 2D case is more complex but I deduced it by taking the formula from this page: http://geometryatlas.com/entries/166, filling in 90 degrees for theta, and filling in 1 for the edges next to that. I actually deduced the formula that takes the squared distances and outputs a new squared distance, and that formula ended up being

$$ d^2=\frac{1}{2}\bigg(a^2+b^2+\sqrt{-(-2+a^2)^2+2(2+a^2)b^2-b^4}\bigg) $$

where you sub in one distance squared for $a^2$ and the other distance squared for $b^2$ (a is one distance, b is the other, but the formula doesn't require you take the square root because it has only even powers of them) and get the resulting $d^2$.

This can be visualized in the following figure. It's a quadrilateral, where the angle at the point you're trying to calculate is 90 degrees, the lengths of the (orange) edges forming that angle are 1, $a^2$ and $b^2$ (red and green) are known, and you want d^2 (purple).

enter image description here

The formula works for this (knowing only 4 and 2 you can get 5), but I also need the formula for the 3D case: where you aren't in the same cardinal plane as the light source, and you need to know your distance squared from the light source based on the three "previous" blocks' distances squared. Basically the same problem as for the 2D case, but instead of a quadrilateral you have a triangular bipyramid where all three edges from the point you're trying to calculate are of length 1 and are 90 degrees to each other, and the lengths of the three edges from the ends of those edges going to the light source are known.

Here's a figure that hopefully visualizes the 3D case sufficiently:

enter image description here

Any pointers are appreciated. Thanks in advance!

EDIT: Managed to solve this myself (with some help from Wolfram Alpha), so I thought I'd go ahead and post the solution in case it helps anyone else: https://www.wolframalpha.com/input/?i=solve+%7Ba%5E2%3D%28x-1%29%5E2%2By%5E2%2Bz%5E2%2C+b%5E2%3Dx%5E2%2B%28y-1%29%5E2%2Bz%5E2%2C+c%5E2%3Dx%5E2%2By%5E2%2B%28z-1%29%5E2%7D+for+x%2Cy%2Cz

$d^2=x^2+y^2+z^2$ for the x, y, and z from there. Should also easily generalize to arbitrary numbers of dimensions.