AMH
AMH

Reputation: 6451

projection of point on line in 3D

I have point with x,y,z

and line direction x,y,z

how to get the point projection on this line

I tried this code

http://www.zshare.net/download/93560594d8f74429/

for example when use the function intersection in the code I got the line direction is (1,0,0) and the point (2,3,3) will have projection (value in x , 0, 0 ) and this is wrong value

any suggestion Best regards

Upvotes: 2

Views: 16012

Answers (2)

Richard Tingle
Richard Tingle

Reputation: 17226

This topic is clearly old and I think the original poster meant vector not line. But for the purposes of Google:

A line, unlike a vector does not (necessarily) have its origin at (0,0,0). So cannot be described just by a direction, it also needs an origin. This is the zero point of the line; the line can extend beyond and before this point, but when you say you’re zero meters along the line this is where you mean.

So to get the projection of a point onto a line you first need to convert the point into the local co-ordinate frame, which you do by subtracting the origin from the point (e.g. if a fence post is the ‘line’ you go from GPS co-ordinates to ‘5 metres to the north and a meter above the bottom of the fence post‘). Now in this local co-ordinate frame the line is just a vector, so we can get the projection of the point using the normal dot product approach.

pointLocalFrame = point– origin

projection = dotProduct(lineDirection, pointLocalFrame)

NOTE: this assumes the line is infinite in length, if the projection is greater than the actual line length then there is no projection

NOTE: lineDirection must be normalised; i.e. its length must be 1

NB: dot product of two vectors (x1,y1,z1) and (x2,y2,z2) is x1*x2+y1*y2+z1*z2

Upvotes: 8

Ricky Bobby
Ricky Bobby

Reputation: 7608

You want to project the vector (x,y,z) on the line with direction (a,b,c).

If (a,b,c) is a unit vector then the result is just (x,y,z).(a,b,c) (a,b,c) = (ax+by+cz)(a,b,c)

If it's not a unit vector make it one, divising it by its norm.

enter image description here


EDIT : a little bit of theory:

Let E be your vectorial space of dimension N:

let F be the line directed by vector a. The hyperplan orthogonal to F is : enter image description here

Now let's chose a vector x in E, x can be writen as : enter image description here where xF is the coordinate of x in the direction of F, an x orthogonal is the coordinate on the orthogonal hyperplan.

You want to find xF: (it's exactly the same formula as the one I wrote above)

enter image description here

You should have a close look at the wikipedia article on orthogonal projections and try to find more stuff on the web .

You can generalise that to any F, if it's not a line anymore but a plan then take F orthogonal and decompose x the same way...etc.

Upvotes: 10

Related Questions