# How to find two perpendicular lines given a single plane

Hey guys, i was wondering how i would go about finding the perpendicular lines relative to a surface normal.

For instance say i have (0,0,1) the expected output would be (1,0,0) and (0,1,0). What would be the best way of achieving this?

I realize that i will be using the cross product, but that requires two vectors, how do i get my first perpendicular line?

Thanks!

Firstly, the vectors you find will not be unique. Any two perpendicular vectors in the plane can be rotated to find two more.

To find an arbitrary pair of perpendicular vectors, just find another vector that is not a scalar multiple of your normal. You can do this by adding a constant value to one of your components (check that the other two aren't zero -- if they are, then add the constant to one of the zero components). Now that you have this other vector compute the cross product of this vector with the normal to get a vector in the plane. Then compute the cross product between this vector and the normal to get a second vector in the plane - and you are done.

how do i get my first perpendicular line?

1. Cross the normal with any arbitrary vector. We'll call this result1 and it is on the plane & perp to the normal.
2. Cross the normal & result1 and this result (result2) is perpendicular to both normal & result1.

If the normal & the arbitrary vectors are unit length, and you normalize result1, your results will be a basis for an orthonormal rotation matrix.

edit - for the arbitrary vector, make sure it's not parallel to the normal.

If any of your normals have 0 in them, then a perpendicular vector will have a non zero component in that direction. IE (0,1,1) will be perpendicular to (x,0,0) for all x!=0. Other than that you can try (y,-x,z) for a perpendicular vector.

Also, lines are not vectors, so you are really finding perpendicular vectors.

• I realize i am finding directional vectors, but what if none of my vectors contain a zero component
– Alex
Sep 20, 2010 at 17:26
• The zero component is the edge case for the second part of my post (Switching two components and negating one). The only difference between my answer and the others is that this vector is for sure on the plane already but requires a check that you aren't switching or negating 0s. Sep 20, 2010 at 22:12

One common application of this is to create a 'look-at' matrix for a camera

In this case you can start with your camera direction vector, and the world up vector as your second vector, and do something along the lines of:

vForward = normalize( vAt - vFrom )
vRight   = normalize( cross( vForward, vWorldUp ) )
vUp      = cross( vRight,   vForward )


And those vectors become the 3 rows of a 3x3 rotation matrix.

Beware of the case where vForward == vWorldUp!