Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
Share on other sites

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 this post

Link to post
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

Let C=S-P

You need to find l that

If you especially interested in how to take derivatives(in straighforward way):
derivative is
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,

We can eliminate "-" :

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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
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 this post

Link to post
Share on other sites
Guest Anonymous Poster
Nothing wrong with homework questions, especially when your teacher sucks.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!