How do I know if two vectors are near parallel

Josh C. picture Josh C. · Sep 27, 2011 · Viewed 11.2k times · Source

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.

Answer

Howard picture Howard · Sep 27, 2011

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.