# How can I project a 3D point onto a 3D line?

Let's say I have a line defined by two points, A and B, both in the form (x, y, z). These points represent a line in 3D space.

I also have a point P, defined in the same format, that isn't on the line.

How would I calculate the projection of that point on to the line? I'm aware of how to do this in 2D but 3D seems to have bugger all resources on it.

You simply need to project vector AP onto vector AB, then add the resulting vector to point A.

Here is one way to compute it:

A + dot(AP,AB) / dot(AB,AB) * AB


This formula will work in 2D and in 3D. In fact it works in all dimensions.

• thank you mr Sam - how did you derive the above formula? – BKSpurgeon May 4 '17 at 5:31
• I did not derive it, it is a well known formula you can find in many handbooks. – sam hocevar May 4 '17 at 12:13
• Is there a sample on how to write that in a programming language like C++? – Vinicius Rocha Jul 5 '19 at 14:47
• @ViniciusdeMeloRocha dot would be a.x*b.x+a.y*b.y+a.z*b.z ... everything else is as straightforward as per-coordinate operation between vectors. – Ocelot Aug 12 '19 at 1:26

Here's a fast and easy way to do it in python:

from numpy import *
def ClosestPointOnLine(a, b, p):
ap = p-a
ab = b-a
result = a + dot(ap,ab)/dot(ab,ab) * ab
return result


Use floats; If your vectors contain integers the division will be an integer division, and the results will be incorrect.

• /dot(ab,ab) is redundant – Waldo Bronchart Jul 4 '17 at 23:20