Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two lines which I´d like to know whether they are parallel or not in 3D space. Each line is defined using two points $(x_1,y_1,z_1)$,$(x_2,y_2,z_2)$. Important condition is that there should be a slight rotation threshold allowed, i.e. if the angle between the two lines is < 5 degrees then they are still parallel.

My idea is to compare the slopes of the two line segments somehow? Another way is to find the direction/normal of the line segment, and compare the two directions using the dot product

Any hints?

share|cite|improve this question
It depends on your application really. You could normalize the directions and use the dot product. When the two lines are almost collinear, the dot product is insensitive to small changes of direction (because of the behavior of $\cos$). – copper.hat Sep 11 '12 at 17:11
up vote 3 down vote accepted

Create vectors pointing along each line by computing $(x_2,y_2,z_2)-(x_1,y_1,z_1)$ for both pairs. Make them into unit vectors by dividing them by their lengths. Call these two unit vectors $u$ and $v$.

Then you can use the inner product identity: $\langle u,v\rangle=\cos(\theta)$, where $\theta$ is the angle between the two vectors.

You want to create two small thresholds around 1 and -1. When the dot product is close to 1, this means that the vectors are very nearly pointing in the same direction, and when the dot product is nearly -1, they are very close to pointing in opposite directions. In both cases, they are "nearly parallel".

share|cite|improve this answer

Natural approaches would be to find two vectors $\vec{a}, \vec{b}$ on the lines ant then either check $\Big|\frac{\vec{a} \cdot \vec{b}}{|\vec{a}| |\vec{b}|}\Big| > \cos \epsilon$ or $\Big|\frac{\vec{a} \times \vec{b}}{|\vec{a}| |\vec{b}|}\Big| < \sin \epsilon$, where $\cdot$ is the dot product, $\times$ is the cross product and $\epsilon$ is the angle of error.

share|cite|improve this answer
but how do u find vectors two vectors a ,b? – Moataz Elmasry Sep 11 '12 at 23:03
@MoatazElmasry, If a line is defined by two points, take the difference of their coordinates. – Karolis Juodelė Sep 12 '12 at 5:52
shouldnt this be normalized by the line length? – Moataz Elmasry Sep 12 '12 at 14:24
i.e. something like ((x_2,y_2,z_2) - (x_1,y_1, z_1))/ length – Moataz Elmasry Sep 12 '12 at 14:57
I normalize wile taking the products. Notice the $|\vec{a}||\vec{b}|$ at the bottom of either inequality. – Karolis Juodelė Sep 12 '12 at 18:56

You can also do this using direction cosines. A vector is defined as r= x i + y j + z k. The direction cosines of r are l=cosα= x/|r|, m=cosβ=y/|r| n=cosγ=z/|r|. Find direction cosines for both vectors and if the direction cosines are equal, then you've just proved the lines are parallel.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.