3D Distance Calculator

Find distance, midpoint and direction between two points on a rotatable 3D graph.

Quick Answer

Straight-line distance in 3D is √(Δx² + Δy² + Δz²). The Δ values are the changes along the three coordinate axes.

Calculate distance

Point A
Point B

Use one unit. Decimals and scientific notation such as 3e-8 work.

Straight-line distance

13 units
Midpoint
(2.5, 4, 9)
Displacement A → B
(3, 4, 12)
A to the origin
3.74165738677
B to the origin
16.6433169771
Show the calculation
  1. Coordinate changes (Δx, Δy, Δz) = (3, 4, 12)
  2. Horizontal diagonal √(Δx² + Δy²) = 5
  3. Space diagonal √(Δx² + Δy² + Δz²) → 13
3D coordinate view
Rotatable 3D plot of points A and BPoint A (1, 2, 3), point B (4, 6, 15) and midpoint (2.5, 4, 9). The solid line is the straight segment. The dashed legs show the coordinate changes when enabled.

Direction cosines and axis angles

cos α · x-axis
0.230769230769
α = 76.6576362029°
cos β · y-axis
0.307692307692
β = 72.0797868606°
cos γ · z-axis
0.923076923077
γ = 22.619864948°

cos² α + cos² β + cos² γ = 1

Coordinates and units

The Euclidean distance in 3D is the length of the straight segment between two points in flat space.

The x, y and z axes

A point is an ordered triple (x, y, z). The axes are perpendicular and meet at O(0, 0, 0). Use one coordinate frame and one length unit for both points.

Distance formula and proof

Subtract A from B axis by axis to get the signed changes Δx, Δy and Δz.

Apply Pythagoras twice

  1. Use the Pythagorean theorem on the horizontal base. The squared base diagonal is Δx² + Δy².
  2. Combine that diagonal with the perpendicular vertical change. Add Δz² to the squared base diagonal and take the nonnegative square root.
d= Δx2+ Δy2+ Δz2
Two right triangles show 3 squared plus 4 squared equals 5 squared, then 5 squared plus 12 squared equals 13 squared.
The base diagonal becomes one leg of the second right triangle.

Midpoint and section formula

Average the two x-values, then the y-values and z-values. Each component of M lies halfway between its corresponding endpoints.

M = ((x₁ + x₂)/2, (y₁ + y₂)/2, (z₁ + z₂)/2)

Recover a missing endpoint

If you know A and M, double each component of M and subtract the corresponding component of A. In vector notation, B = 2M − A.

Divide a segment in a given ratio

For a point P between A and B with AP : PB = m : n, compute P = (nA + mB)/(m + n) component by component. Equal weights give the midpoint.

Displacement, direction cosines and unit vectors

Displacement from A to B

The displacement (Δx, Δy, Δz) records how far and in which direction you move along each axis. Its length is the distance d.

Angles to the positive axes

For a nonzero displacement, cos α = Δx/d, cos β = Δy/d and cos γ = Δz/d. Use arccos to get the corresponding angles in degrees. The squares of the three cosines add to 1.

Unit vector

The three cosines form the components of the unit vector from A to B. If the endpoints coincide, d is zero and the axis angles, direction cosines and unit direction are undefined.

The displacement from A(0, 0, 0) to B(1, 2, 2) is (1, 2, 2). The reverse displacement is (−1, −2, −2). Both have length 3.
Swapping the endpoints reverses the displacement and the unit vector. Both distances are 3 units.

Distance to the origin

Use O = (0, 0, 0) as one endpoint. A point P(x, y, z) is √(x² + y² + z²) units from O.

This length is the norm of the position vector. It is the radial distance in spherical coordinates and the radius of a sphere centered at O that passes through P.

Six worked examples

Equal z-coordinates

From A(1, 2, 3) to B(4, 6, 3), the changes are (3, 4, 0). The distance is √25 = 5 units.

Changes along all three axes

From A(1, 2, 3) to B(4, 6, 15), the changes are (3, 4, 12). The base diagonal is 5, and the space diagonal is √(25 + 144) = 13 units.

Distance from the origin

For P(2, 6, 9), the squared distance is 4 + 36 + 81 = 121. The length is 11 units.

Distance, midpoint and direction

From (0, 0, 0) to (1, 2, 2), d = 3, M = (0.5, 1, 1) and the unit vector is (1/3, 2/3, 2/3). The corresponding angles are about 70.53°, 48.19° and 48.19°.

Negative coordinates

From (−3, −4, −5) to (1, 2, 7), the changes are (4, 6, 12). Then √(16 + 36 + 144) = 14 units.

Convert units before calculating

For offsets of 3 m, 4 m and 1,200 cm, convert the last offset to 12 m. The length is 13 m. Use the length converter when the coordinates use different units.

A rectangular box has perpendicular edge lengths 3, 4 and 12, a base diagonal of 5, and a space diagonal of 13.
A 3–4–12 box has a base diagonal of 5 and a space diagonal of 13.

How 3D distance relates to 2D distance

When both points have the same z-coordinate, Δz is zero and the 3D formula reduces to the 2D distance in their horizontal plane. If Δz is nonzero, it adds to the squared length of the xy projection.

Try the 2D distance calculator for planar coordinates or the distance tools guide for other distance questions.

Distance in four or more dimensions

For Euclidean coordinates in any number of dimensions, add one squared difference per axis and take the square root.

For data vectors, choose feature scales that suit the question before comparing distances. Spacetime intervals use a different formula for the time component.

Where 3D distance is used

  • Satellite navigation. Measure a signal’s range between positions in a common Cartesian reference frame.
  • Orbital mechanics. Determine how far apart vehicles are at a given time. The time–distance tool covers motion calculations.
  • Medical imaging. Convert voxel positions with the image’s physical spacing and orientation before measuring lengths.
  • Graphics and games. Compare the gap between object centers with their collision radii.
  • Robotics. Measure the straight gap to a waypoint when evaluating paths.
  • Structural engineering. Determine the length of a diagonal brace between two joints.
  • Particle detectors. Compare reconstructed spatial positions of detector hits.
  • Astronomy. Use a consistent frame to find a separation. That separation is an input to gravitational force calculations.

Common mistakes

  • Include the height difference. Equal heights give Δz = 0, even when both z-coordinates are nonzero.
  • Keep the subtraction order. Use parentheses around negative coordinates, such as 1 − (−3) = 4.
  • Square each change separately. Add the three squares before taking the root.
  • Choose the right origin calculation. Use all three axes for distance to O. Use only x and y when finding the horizontal projection.
  • Distinguish an angle from its cosine. A cosine of 0.5 corresponds to an angle of 60°.
  • Find a perpendicular projection for a closest point. The midpoint describes the two endpoints of a segment.

Integer distances and precision

Pythagorean quadruples

|Δx||Δy||Δz|Distance
1223
2367
26911
341213
461214

Input and result precision

Enter decimals or scientific notation such as 1.2e-8. Each coordinate can be zero or have a magnitude from 1 × 10−300 to 1 × 10300, with up to 15 significant digits after ignoring trailing zeros. Use a decimal point and omit unit suffixes and commas.

The calculator forms differences and midpoints from the entered decimals, then displays results to 12 significant digits. The plot is a projection of the points, so read lengths from the calculation.

Practice with worked answers

1. Three nonzero coordinate differences

Find the distance and midpoint between A(0, 1, −2) and B(2, 4, 4).

Show worked answer

The changes are (2, 3, 6). Their squared lengths add to 4 + 9 + 36 = 49, so d = 7 units. The averages give M = (1, 2.5, 1).

2. Negative coordinates and an exact radical

Find the distance between A(−4, 3, −2) and B(2, −5, 1). Give an exact answer and a decimal to four places.

Show worked answer

The changes are (6, −8, 3), so d = √(36 + 64 + 9) = √109 ≈ 10.4403 units.

3. Convert the units before calculating

A drone ends 1.5 m east, 200 cm north and 2 m above its starting point. Find its straight-line displacement length.

Show worked answer

Convert 200 cm to 2 m. The three changes are (1.5, 2, 2) m, so d = √10.25 m = (√41 ÷ 2) m ≈ 3.2016 m.

4. Equal heights

Find the distance between A(−2, 1, 7) and B(4, 9, 7). Explain why a 2D calculation works here.

Show worked answer

The changes are (6, 8, 0). Both points lie in the plane z = 7, so the distance is √(6² + 8²) = 10 units.

5. A direction with a negative component

For A(−1, 2, 3) and B(1, −2, 7), find the distance, midpoint, unit vector and three axis angles.

Show worked answer

The changes are (2, −4, 4), giving d = 6 and M = (0, 0, 5). The unit vector is (1/3, −2/3, 2/3). Taking arccos gives approximately 70.53°, 131.81° and 48.19°.

6. Recover the missing endpoint

The endpoint A is (−3, 2, 0) and the midpoint M is (2, −1, 4). Find the other endpoint and the segment length.

Show worked answer

B = 2M − A = (7, −4, 8). The changes are (10, −6, 8), so d = √200 = 10√2 ≈ 14.1421 units.

7. Two possible values of an unknown coordinate

A(1, 2, 3) is 13 units from B(4, 6, z). Find every possible value of z.

Show worked answer

The squared length gives 169 = 9 + 16 + (z − 3)². Therefore z − 3 = ±12 and either z = 15 or z = −9.

8. Identical points

Let A = B = (2, −3, 4). Find their distance, midpoint, displacement and distance from the origin. Is there a unit direction?

Show worked answer

The displacement is (0, 0, 0), the distance is zero, and the midpoint is (2, −3, 4). Both points are √29 ≈ 5.3852 units from the origin. A zero-length segment has undefined direction.

Sources and further reading

  • OpenStax Calculus Volume 3, Vectors in Three Dimensions (openstax.org/books/calculus-volume-3/pages/2-2-vectors-in-three-dimensions)
  • OpenStax Calculus Volume 3, The Dot Product (openstax.org/books/calculus-volume-3/pages/2-3-the-dot-product)
  • OpenStax Calculus Volume 3, Cylindrical and Spherical Coordinates (openstax.org/books/calculus-volume-3/pages/2-7-cylindrical-and-spherical-coordinates)
  • OpenStax University Physics Volume 3, The Lorentz Transformation (openstax.org/books/university-physics-volume-3/pages/5-5-the-lorentz-transformation)
  • NASA, Introduction to Global Navigation Satellite Systems
  • DICOM Standard, Image Plane Module (dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.2.html)
  • Cornell CS4780, k-nearest neighbors (cs.cornell.edu/courses/cs4780/2018fa/lectures/lecturenote02_kNN.html)
  • scikit-learn, Nearest Neighbors (scikit-learn.org/stable/modules/neighbors.html)

Related HeLovesMath tools

Compare a straight-line separation with a curved boundary using the circle circumference calculator. Find more lessons in homework resources.

Advertisement
Practice and background

Separate direction from distance

From the point with coordinates one, two, three to the point four, six, fifteen, the coordinate changes are three, four and twelve. The first two changes make a horizontal displacement of five. Combine five and twelve at right angles to get a distance of thirteen. Reversing the journey reverses each coordinate change but leaves all three squares and the final distance unchanged.

Frequently asked questions

What changes when I swap points A and B?

The midpoint and distance stay the same. The displacement and unit vector reverse, and each axis angle becomes its supplement.

What if A and B have the same coordinates?

The midpoint is the common point and the displacement is (0, 0, 0). The distance to the origin can still be positive.

Can I enter latitude, longitude and altitude?

Convert the positions to a common Cartesian reference frame first. Use a geographic distance method when you need a length measured along Earth’s surface.

Why do some angles show an approximation sign?

A small component can make the true angle just below or above 90°, or just below 180°. The result uses ≈ when rounding displays one of those boundary values.

Can the distance alone identify the second point?

A positive distance gives a sphere of possible points centered at the known endpoint. Extra coordinates or other constraints narrow the possibilities.