Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if two vectors are near parallel

I am having some trouble finding parallel vectors because of floating point precision. How can I determine if the vectors are parallel with some tolerance?

I also need a check for orthogonality with tolerance.

like image 504
Josh C. Avatar asked Sep 27 '11 16:09

Josh C.


People also ask

How do you know if two vectors are parallel or antiparallel?

Parallel vectors have the same direction angles but may have different magnitudes. Antiparallel vectors have direction angles that differ by 180°.

How do you know if vectors are parallel to cross product?

According to the length of the cross product result, two vectors are parallel if and only if their cross product is zero. This is because two vectors are parallel only if and only if their angle is (0 degrees or 180 degrees).


2 Answers

For vectors v1 and v2 check if they are orthogonal by

abs(scalar_product(v1,v2)/(length(v1)*length(v2))) < epsilon

where epsilon is small enough. Analoguously you can use

scalar_product(v1,v2)/(length(v1)*length(v2)) > 1 - epsilon

for parallelity test and

scalar_product(v1,v2)/(length(v1)*length(v2)) < -1 + epsilon

for anti-parallelity.

like image 135
Howard Avatar answered Sep 20 '22 18:09

Howard


If you have 3D vectors the answer is simple. Compute the cross product and if it is nearly zero, your vectors are nearly parallel: http://mathworld.wolfram.com/ParallelVectors.html

For 2d vectors you can convert them into 3D vectors just by adding a coordinate with zero (1;2) => (1;2;0), (4; 5.6) => (4; 5.6; 0) and so on

Two vectors are orthogonal or perpendicular, if there dot product ist zero: http://mathworld.wolfram.com/CrossProduct.html

-edit http://mathworld.wolfram.com/Perpendicular.html

like image 38
yunzen Avatar answered Sep 20 '22 18:09

yunzen