# Distance from point to vector

This topic is 4877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been trying to find a formular for a distance from a point to a vector, but have been out of luck. There are several formular examples of distance from point to line, but what I have is a vector starting position, a vector direction and a point in 3D space. Can anyone point me to a formular for this calculation ?

##### Share on other sites
if i remember correctly, and assuming you have 2 co-ordinates in 3d space...

absolute value = sqrt ( x^2 + y^2 + z^2 )

so...distance = point2 - point1, then find absolute value of that answer

been 6 years since i touched this stuff, can anyone back me up/correct me?

##### Share on other sites
You can still apply the formula for the distance between point and line.
To find the equation of the line passing through your starting point S=(s.x,s.y,s.z) with direction of the vector v=(v.x, v.y, v.z) you have to do the following.

E=(e.x,e.y.e.z) is another point you can find this way:
E=v*k+S, for your favourit (non zero) value of k ;)
For example, let k be 5, you will have E=(s.x+5*v.x, s.y+5*v.y, s.z+5*v.z).

Now you have 2 point in the vector direction, and precisely on the line parallel to the vector (same direction) and passing through your starting point S (and E).
You can apply the formula to find a line knowing 2 of its points (it's the intersection of 2 3d planes):

(x-e.x)/(s.x-e.x)=(y-e.y)/(s.y-e.y)=(z-e.z)/(s.z-e.z)

Now you should be able to compute the distance from point to line.

Hope this helps.

##### Share on other sites
You can't have a "vector starting position", vectors have direction and magnitude, but no position...

##### Share on other sites
Quote:
 Original post by Mortwhat I have is a vector starting position, a vector direction and a point in 3D space.

Quote:
 Original post by dotproductYou can't have a "vector starting position", vectors have direction and magnitude, but no position...

dotproduct you're right. I know that. But I understand Mort has a point where the vector can be applied to. For example, I have the vector v=(1,2,3) (with no point).
If I have a point P=(a,b,c) the vector v applied to the point P is (1+a,2+b,3+c)
and that is another vector and another 3d point, say Q. Now P and Q are points of a 3d line that has the same direction of the vector v.
That's what I meant (sorry I've not explained clearly [embarrass]).

##### Share on other sites
Quote:
 Original post by MortI have been trying to find a formular for a distance from a point to a vector, but have been out of luck. There are several formular examples of distance from point to line, but what I have is a vector starting position, a vector direction and a point in 3D space.

Your terminology is a bit wrong here. There is no such thing as the distance from a point to a vector. A vector (aka a free vector) does not have a location and therefore you cannot talk about distance to or from a vector.

You say that in addition to the 3D point (lets call it P), you have a starting position (lets call it O) and a direction vector (lets call it D). What O and D describe is a _ray_ (lets call it R). We can write R in parameterized form as:

R(t) = O + t*D, t >= 0

Really, then, what you're asking for is the distance between the point P and the ray R(t) = O + t*D, t >= 0.

To solve this problem, you need some elementary linear algebra skills. There are two cases:

1. When we consider the orthogonal projection (lets call it Q) of P onto the (infinite) line L that is parallel to the ray, Q does not lie on R. That is, Q lies in the opposite direction of D from the start position O. In this case, O is clearly the closest point on R to P. The _squared_ distance between P and O is then given by d2 = Dot(P - O, P - O).

2. When Q projects onto R, the squared distance between P and R is the squared distance between P and Q (because the projection was orthogonal). The squared distance is then given by d2 = Dot(P - Q, P - Q). Without derivation or further explanation, Q itself is given by Q = O + (Dot(P - O, D) / Dot(D, D))*D.

Case 1 occurs when Dot(P - O, D) <= 0 (that is, when the angle between the vectors OP and D is obtuse), and otherwise Case 2 occurs.

In code, this becomes something like:

// Given:// Point P, O;// Vector D;Vector T = P - O;float t = Dot(T, D), d2;if (t <= 0.0f) {    d2 = Dot(T, T);} else {    Point Q = O + (t / Dot(D, D)) * D;    T = P - Q;    d2 = Dot(T, T);}return d2;

Two last things:

First, this returns the squared distance, because it's more convenient to work with. If you need the actual distance, you need to return sqrtf(d2).

Second, if you know that your vector D is of unit length (length of one) then "Dot(D, D)" will be one as well and you can remove the division by that term (just keeping the "t" bit).

Hope this helps clear things up for you a bit.

##### Share on other sites
Thanks for your help solving this problem.

I went with the method that Christer Ericson provided, mainly because it takes into account that the ray has a starting position but no stopping position (Sorry Christer, I don't know how to express that in correct mathematical terms).

I tried to understand what happened in the formular, but math is not my strong side so after trying to understand every part of it for some hours, I ended up just using the code sample you provided (Thanks for providing that too).

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631424
• Total Posts
3000010
×