# Determining 3D Distances and Angles

Near the top of one of the sections in the POV-Ray™ 3.5 manual there is a quote about wishing that past mathematical formulas were remembered, or something to that effect. I too wish I had remembered what I had learned in some of my past math classes, but after a bit of research, I have begun to work on some of the information that I have forgotten. Here is one of those areas.

### Determining 3D Distances:

Given two points, point A (located at <x_{A}, y_{A}, z_{A}>) and point B (located at <x_{B}, y_{B}, z_{B}>), the distance between these two points (AB) can be determined by the formula:

|AB| = √[(x_{B} - x_{A})^{2} + (y_{B} - y_{A})^{2} + (z_{B} - z_{A})^{2}]

In other words, take each x/y/z coordinate and subtract it from the other (remembering that if you take B-A you must continue to do so in that order). Then, square the value for each coordinate and add them all together. Taking the square root of that sum will give you the distance from the second point to the first. Simply take the absolute value of the square root to get the absolute distance.

For example:

**Example One:** Given point A <5, 0, 0> and point B <6, 7, 0>, what is the distance between these two points?

How to do it: First, we take 5 from 6, 0 from 7, and 0 from 0. (6-5) = 1, (7-0) = 7, (0-0) = 0

Square each term and add them together: 1^{2} + 7^{2} + 0^{2} = 1 + 49 + 0 = 50

Take the square root of the resulting term: √(50) = 7.071 or √(5 * 5 * 2) = 5√(2)

**Example Two:** Given point A <1, 7, -3> and point B <-5, 4, -2>, what is the distance between these two points?

How to do it: First, we take 1 from -5, 7 from 4, and -3 from -2. (-5-1) = -6, (4-7) = -3, (-2--3) = (-2 + 3) = 1

Square each term and add them together: -6^{2} + -3^{2} + 1^{2} = 36 + 9 + 1 = 46

Take the square root of the resulting term: √(46) = 6.782

Knowing how to determine the distance between two three-dimensional points is necessary to determine 3D angles ...

### Determining 3D Angles:

Determining 3D angles can be helpful in POV-Ray™, if, for instance, you would like to view the scene from a certain angle and would also like to know the respective distances. It would be a good idea to know how to determine 3D distance in order to get all that you can from this...

First of all, it is important that you remember, or have knowledge of soh-cah-toa. Id est:

sin(*angle*) = [(opposite length) / (hypotenuse length)]

cos(*angle*) = [(adjacent length) / (hypotenuse length)]

tan*(angle*) = [(opposite length) / (adjacent length)]

So, given two points, one could make a box from those two points (even if the box has 0 length/width/depth). Any box can be split into two triangles of the same basic shape. A triangle has three angles and three sides. Using the formulas above, if given one angle and one side, we could determine all other angles and sides. So, if we have one corner (which is the look at point for instance) and another corner (which is the camera for instance) we could easily determine the angle(s) between them. How about an example:

- A camera situated at <5, 5, -5> is looking at the origin <0, 0, 0>. What is the angle that the camera is looking at the origin?
- There are two ways to go about solving this problem, however, for this page I will stick with one way.
- Already we have one side of the triangle. The camera is located 'up' 5 points from the origin. Therefore, one side of the triangle has a length of 5.
- By determining the distance from the camera to the origin, which is 5√(3), we have yet another length. This length is the hypotenuse length, since it is the side opposite of the 90° angle.
- Now, it is easiest to use the angle at the origin (in this case, it is an angle that opens above the positive x-plane, negative z-plane, and into the positive y-plane. Right, up, and back...
- The opposite side would be the y-coordinate that we determined in #2, which is 5. The hypotenuse length is what we determined in #3, which is 5√(3). Since we have the opposite length and hypotenuse length, we would take the inverse-cos to determine the angle.
- So, cos
^{-1}{(5)/[5√(3)]} = cos^{-1}{(1)/[1√(3)]} = cos^{-1}(0.5774) = 54.73561°. - However, this is the angle from the origin to the camera, so if we wanted to determine it from the other way, we would simply take the angle * -1, or switch the sign in the front of the angle.
- We could also determine #6 from the camera. However, the opposite length would become the adjacent length and the hypotenuse length would stay the same. We would also have to take the inverse-sin of the value.
- So, sin
^{-1}{(5)/[5√(3)]} = sin^{-1}{(1)/[1√(3)]} = sin^{-1}(0.5774) = 35.26439°. Taking this from 90 (as this value is the angle from the y to the xz, instead of the xz to the y, which is what we really want) we get our correct value of 54.73561°. - In other words and using the image below, "look at pt" is the origin, "look from pt" is the camera, "h" is the distance determined in #3, "o or a" was not determined in this example, and "a or o" was the distance determined in #2. The angle determined in #6 would be the angle at "look at pt", while that determined in #9 would be the angle
__inside__the triangle at "look from pt", followed by the angle__outside__the triangle at "look from pt".

From look at pt, (a or o) would be opposite and (o or a) adjacent.

From look from pt, (a or o) would be adjacent and (o or a) would be opposite.

**Primarily Written/Added:** August 14th 2002/October 17th 2002

**
Edited/Updated:** November 21st 2002; March 6th 2003; November 5th 2003; September 4 2006.

Thanks to Dave Cousineau for pointing out a mistake that stuck around for over 4 years :D

##### Search

##### Links of Note

##### Recent Posts

##### Support This Site

If my blog was helpful to you, then please consider visiting my Amazon Wishlist.