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.
Parallel vectors have the same direction angles but may have different magnitudes. Antiparallel vectors have direction angles that differ by 180°.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With