# How do I find the shortest distance from a point to a fixed

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

## Recommended Posts

How do I find the shortest distance from a point to a fixed line? I know it involves using the distance formula and setting the derivative to zero, but I am stuck on a problem. Does any one know of any good tutorials? I have been googling it to really no such avail.

##### Share on other sites
Google knows all. ...and yes, almost all of those results are relevant.

##### Share on other sites
Hello!

Let's say that we have a point q and a line L. L is described as follows :

p . n = d // where "." is a dot product of vectors p and n

It means that for arbitrary point p which belongs to a line the equation is satisfied. 'n' is a normal vector of a line (a vector perpendicular to a line, which basically determines the direction of a line's surface). 'd' is the distance from the line to the origin. Now, to calculate the shortest distance from the point q to L all you need to do is :

distance = d - q * n;

If you don't feel like it was clear enough (I really doubt if my explanation was good :P ), you should think about getting yourself a book on 3D maths or something. You can also visit www.gametutorials.com and there is some math introduced with OpenGL tutorials.

Hope it helps! :)

---------------------------

##### Share on other sites
Differenciating is relatively stupid way to solve that problem.
In closest point you just have DotProduct(line_direction,vector_to_point) = 0 .You can use it instead of derivative (that DotProduct is exactly equal to derivative you want.I just differenciated in mind :-). In fact, what you solve for zero does not have to be necessarily derivative.

It's very simple to solve.

Let your point is P. Let your line start point is S and direction is D.
Line equation is
S+D*l
.

Let C=S-P

You need to find l that
DotProduct(D,C+D*l)=0

If you especially interested in how to take derivatives(in straighforward way):
derivative is
||C+D*l||'=0
where it doesn't matter if ||X|| is a just length of X or squared length. Let it be squared length.
Squared length of vector is equal to summ of squared components.

So we have
(C.x+D.x*l)2)' + [same blablabla for y, z, etc.] =0.

Where ' mean derivative by l.

Expanding & differenciating, it's turns into

2*C.x*D.x+2*D.x2*l + [same blabl....] =0

2* can be removed.

C.x*D.x+D.x2*l + [same blabl....] =0

so it's

DotProduct(C,D) + DotProduct(D,D)*l = 0
(it's also equal to DotProduct(D,C+D*l) )

From there,

l=-DotProduct(C,D)/DotProduct(D,D);
We can eliminate "-" :
l=DotProduct(P-S,D)/DotProduct(D,D);

edit: note that if direction is normalized,you don't need to divide and so it's JUST ONLY l=DotProduct(P-S,D) ,and it's one of definitions of dot product to give that result.

To find distance to finite line segment,let D=EndPoint-StartPoint. Just check if your l is in range 0..1 ( .If l>1,EndPoint is closest,if l<0,StarPoint is closest. You may just clamp to 0..1. )

##### Share on other sites
It's a Calculus problem in my homework so I can't use vectors.

##### Share on other sites
ops. Homework :( .

Mods,sorry for explanations .... next time i'll try to make sure it's not homework ....

edit:and i must say,i hate homework questions like everyone else there, and i especially don't like if people that ask homework questions says something like "oh no,i can't use vectors", etc, and don't even bother to write vectors expanded himself. edit: ! ! ! !.

##### Share on other sites
Oh boy...This is a no-no.

Mr Rhodes greatly disapproves (and is quite strict in his disallowance of such) of these types of threads and will probably close it soon. You should not have used the forbidened word Homework. I won't point you to google but I will point out two links that look good from there and suggest making it your first stopping point regarding inquiries similar to this.

Look here and if it is not what you wish there is also MathWorld, a good reference site although you have to already understand the concepts. I do not believe you can actually teach yourself from scratch from it...

##### Share on other sites
Forum FAQ

Look at all the bright yellow text. You can't miss it, really.

##### Share on other sites
As you can see by the incurred reactions Mr Rhodes is quite strict in his treatment of requests for homework aid. You could have gotten away with this had you not pointed out that this was a homework problem (and stated in a way also, that displayed a seeming lack of willingness to think for yourself) as this sounds like a problem applicable to game programming.

So in the future if you wish to sneak such questions in, state it in terms of a programming problem.

EDIT: As for not being allowed to use vectors, translate the concepts. You need not work the solutions in vector format.

Hint: If you do not already know it, look up the definition of the dot product. And also recall what the definition of the derivative at a point is...That matches your above estimation of the process no?

##### Share on other sites
Nothing wrong with homework questions, especially when your teacher sucks.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013441
×