This guide focuses on accurate midpoint calculation in 3D space using Cartesian coordinates. It covers distance evaluation, parametric representation of a segment, point projection, and practical use cases in graphics, engineering, and simulation workflows.
Table of Contents
What this tool does
- Accepts two points A with coordinates Ax, Ay, Az and B with Bx, By, Bz.
- Calculates the midpoint M, the Euclidean distance |AB| and displays those values.
- Renders the segment in 3D using a thin cylinder and markers for endpoints and midpoint.
- Supports interactive coordinate edits and an automatic camera fit to the geometry.
- Works as a 2D midpoint finder by keeping both Z coordinates equal.
Fundamental formulas
Midpoint coordinates
Given points
$$A = (A_x, A_y, A_z),$$
$$B = (B_x, B_y, B_z),$$
The midpoint M is the average on each axis
$$M = (M_x, M_y, M_z) =$$
$$=\left(\frac{A_x + B_x}{2},\; \frac{A_y + B_y}{2},\; \frac{A_z + B_z}{2}\right)$$
Segment length
Compute axis differences
$$\Delta x = B_x – A_x,$$
$$\Delta y = B_y – A_y,$$
$$\Delta z = B_z – A_z.$$
Then Euclidean length
$$
|AB| = \sqrt{(\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2}.
$$
Vector and parametric form
$$\vec{v} = \overrightarrow{AB} =$$
$$= (B_x-A_x,\; B_y-A_y,\; B_z-A_z),$$
$$\hat{u} = \frac{\vec v}{\|\vec v\|}$$
$$P(t) = A + t(B-A) =$$
$$= (A_x + t\Delta x,\; A_y + t\Delta y,\; A_z + t\Delta z)$$
The midpoint corresponds to t = 0.5.
Projecting an external point onto the segment
For a point Q the projection parameter is
$$t^* = \frac{(Q-A)\cdot (B-A)}{(B-A)\cdot(B-A)}$$
Clamp t to the interval [0,1] to get the nearest point on the segment
$$
t = \mathrm{clamp}(t^*,0,1),$$
$$
P(t) = A + t(B-A).
$$
Quick reference table
| Operation | Formula | Note |
|---|---|---|
| Midpoint | \(M=\big(\frac{A_x+B_x}{2},\frac{A_y+B_y}{2},\frac{A_z+B_z}{2}\big)\) | Axis-wise average |
| Length | \(\sqrt{(\Delta x)^2+(\Delta y)^2+(\Delta z)^2}\) | Euclidean distance |
| Parametric | \(P(t)=A+t(B-A)\) | Use for interpolation and animation |
| Unit direction | \(\hat u=\frac{B-A}{\|B-A\|}\) | Needed for normals and projections |
Step-by-step examples
Example 1 — integer coordinates
Given A = (0, 0, 0) feet and B = (10, 20, 30) feet compute midpoint and length.
Midpoint calculation
$$
M_x = \frac{0+10}{2} = 5,
$$
$$
M_y = \frac{0+20}{2} = 10,
$$
$$
M_z = \frac{0+30}{2} = 15.
$$
Resulting midpoint M = (5, 10, 15) feet.
Length calculation
$$
\Delta x = 10,\;\Delta y = 20,\;\Delta z = 30.
$$
$$
|AB| = \sqrt{10^2+20^2+30^2} =
$$
$$
= \sqrt{100+400+900}
\approx 37.4\ \text{feet}.
$$
Example 2 — decimals and negatives
Given A = (2.5, -1.0, 0.75) and B = (6.1, 3.2, -1.25) compute midpoint and length.
Sums by axis
$$
A_x+B_x = 2.5+6.1 = 8.6,
$$
$$
A_y+B_y = -1.0+3.2 = 2.2,
$$
$$
A_z+B_z = 0.75+(-1.25) = -0.5.
$$
Midpoint
$$
M = (8.6/2,\;2.2/2,\;-0.5/2) =
$$
$$
= (4.3,\;1.1,\;-0.25).
$$
Length
$$
\Delta x = 3.6,\;\Delta y = 4.2,\;\Delta z = -2.0.
$$
$$
|AB| = \sqrt{3.6^2+4.2^2+(-2.0)^2} =
$$
$$
= \sqrt{12.96+17.64+4.00}
\approx 5.882.
$$
Implementation notes and best practices
- Keep all coordinates in the same unit system.
- Use double precision to reduce round-off errors.
- Handle zero-length segments to avoid division by zero.
- Clamp projection parameters for stable nearest-point queries.
- Scale visual markers relative to segment length.
- Prefer vector math utilities in performance-critical code.
Sample code snippets
// Inputs: Ax, Ay, Az, Bx, By, Bz
Mx = (Ax + Bx) / 2;
My = (Ay + By) / 2;
Mz = (Az + Bz) / 2;dx = Bx – Ax;
dy = By – Ay;
dz = Bz – Az;
length = Math.sqrt(dx*dx + dy*dy + dz*dz);Px = Ax + t * dx;
Py = Ay + t * dy;
Pz = Az + t * dz;t_star = ((Qx-Ax)*dx + (Qy-Ay)*dy + (Qz-Az)*dz) / (dx*dx + dy*dy + dz*dz);
t = Math.max(0, Math.min(1, t_star));
ProjX = Ax + t * dx;
ProjY = Ay + t * dy;
ProjZ = Az + t * dz;
Testing checklist
- Zero-length segment. Resulting midpoint equals endpoints.
- Large coordinates. Verify no overflow and acceptable precision.
- Projection edge cases where Q projects exactly to endpoints.
- UI values sync between sliders and numeric inputs.
The formulas and techniques above let you compute a reliable midpoint in 3D for geometry, visualization and engineering tasks. Use the parametric form and projection rules to extend functionality. Keep one keyword in mind: midpoint in 3D.
Recommended books
- Fletcher Dunn and Ian Parberry — 3D Math Primer for Graphics and Game Development
- Mark de Berg, Otfried Cheong, Marc van Kreveld, Mark Overmars — Computational Geometry: Algorithms and Applications
- Christer Ericson — Real-Time Collision Detection






