PDA

View Full Version : dumb vector math question---point on a line

max_jenkins
11-14-2002, 10:24 AM
Hi--

so I have my 3d line equation of the form:

P = P1 + k(P2-P1) where Pn is <x, y, z>.

A line can be defined just through the points P1, P2, so how do I solve for k and P in the equation?

Forget the point-in-line test for the moment. Given a known P1, P2, what do I set P equal to to solve for k? can P just be equal to P1 or P2? If this is the case, don't i get different results for k depending on whether or not P = P1 or P2?

Like, if we have P = P1 + k(P2-P1),
can I then say P1 = P1 +k(P2-P1)?
rearranging, that seems like it just becomes P1 - P1/(P2-P1) = k = 0.

alternatively, if i put P = P2, i get the equation P2 = P1 + k(P2-P1), which is equal to (P2-P1)/(P2-P1) = k = 1.

So given that I can define my line with _only_ P1 and P2, what value should i substitute for P to solve for K?

Thanks a lot

Max

Coconut
11-14-2002, 10:41 AM
In most cases, you want to find k, such that the P on the line is closest to the P' you input. A tolerance can be used to determine whether the point is on the line.
Try searching thru google by keywords:
"Point line distance"

nexusone
11-14-2002, 11:25 AM
Let's look at the equation:

P = point on the line(x,y,z)
k = is a number between 0 and 1, 0.5 being midpoint on the line.

If k = 0, then P = P1
If k = 1, then P = p2
If k = 0.5 then P = P1 + 0.5 * (P2 - P1) which translates to this in C code:

P.x = P1.x + 0.5 * (P2.x - P1.x);
P.y = P1.y + 0.5 * (P2.y - P1.y);
P.z = P1.z + 0.5 * (P2.z - P1.z);

I don't see the need to find k, but here is what would be the answer.

We need only solve for one axis:

k = (P.x - P1.x)/(P2.x - P1.x)

Normally we use k for movement between two points on our vector.

ship starts at vector P1 and will end at P2.

k = 0 ship starting point.
k = 1 ship ending point.

k = 0;

loop_start

k +=0.01; Rate of movement.

P = P1 + k * ( P2 -P1 );

draw_ship_at( P );

keep looping until k = 1 ending point.

Originally posted by Coconut:
In most cases, you want to find k, such that the P on the line is closest to the P' you input. A tolerance can be used to determine whether the point is on the line.
Try searching thru google by keywords:
"Point line distance"

[This message has been edited by nexusone (edited 11-14-2002).]