I am having some trouble determining if two line segments are collinear because of floating point precision. How can I determine if the line segments are collinear with some tolerance?
I am having some trouble determining if two line segments are collinear because of
Share
EDITED:
Line segments are colinear if they contain two of the same points. They are near-colinear if they share one point and are near-parallel.
Vectors are effectively parallel if the angle between them is less than a threshold you state. Maybe less than .000027 degrees, which is the decimal equivalent of one tenth of a degree-second (which is in latitudinal distance, and equivalently of longitudinal distance at the equator, a difference of approximately ten feet; that’s about the accuracy of civilian GPS).
You didn’t tell us what language or library you’re using; in .NET’s System.Windows.Media.3D library there is a Vector3D struct which has an AngleBetween() method, making this check a one-liner.
The “basic” math (it’s actually vector trig, not a “basic” concept by most definitions) is that θ=cos-1( A*B / |A||B| ); that is, the arc-cosine of the quantity of the scalar product of the two vectors divided by the product of their magnitudes.
The dot product of vector A and vector B, both containing components X, Y, and Z, is XAXB + YAYB + ZAZB. The magnitude of a vector A is sqrt(XA2 + YA2 + ZA2).
So, in pseudo-C-ish: