Find vector normal to line segment

Started by
2 comments, last by Alessandro 9 years ago

Hello folks, I googled around and I yet can't find a way to get this done.

Suppose I have a line segment going from (x1,y1,z1) to (x2,y2,z2). How do I calculate the normal vector perpendicular to this line?

Thanks for any help.

Advertisement
You don't. There isn't a 'the', there is an infinite amount of possible vectors fulfilling that criterion. One simple way out is to pick another vector (which must not be collinear with your input direction vector) and take the cross product of your direction vector and your picked vector. The result is perpendicular to your initial direction vector.

Edit: Whoops I thought you were asking a 2D question, I didn't see the z coordinate.

It's important to realize that you're asking to treat the line segment as a mathematical plane, in order to calculate the normal. This means the normal can face in one direction, or the other. An easy way to calculate the normal is to rotate the segment CCW, or rotate it CW, followed by a normalization.

CCW Example:

n = Vec2( seg.y, -seg.x );

The above comes from writing down the 2x2 rotation matrix for a 90 degree CCW rotation, where the entries of the matrix are either sin or cos of 90. When we multiply a vector with this matrix we get the above result.

Thanks a lot for the explanations!

This topic is closed to new replies.

Advertisement