vectorparallel-processingdot-productcross-product# How do I know if two line segments are near collinear

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?

Solution

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 X_{A}X_{B} + Y_{A}Y_{B} + Z_{A}Z_{B}. The magnitude of a vector A is sqrt(X_{A}^{2} + Y_{A}^{2} + Z_{A}^{2}).

So, in pseudo-C-ish:

```
//Vector is a simple immutable class or struct containing integer X, Y and Z components
public bool CloseEnough(Vector a, Vector b, decimal threshold = 0.000027m)
{
int dotProduct = a.X*b.X + a.Y*b.Y + a.Z*b.Z;
decimal magA = sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z); //sub your own sqrt
decimal magB = sqrt(b.X*b.X + b.Y*b.Y + b.Z*b.Z); //sub your own sqrt
decimal angle = acos(dotProduct/(magA*magB)); //sub your own arc-cosine
if(angle <= threshold
}
```

- Why do I keep getting Microsoft C++ exception: std::out_of_range at memory location 0x000000B70C8FF360. when using vector?
- Vectors to Priority_queue in STL c++
- Modify the data of SVG path element's d-attribute to make the resulting path flipped
- How to get a 3D vector in the same direction as another vector limited by an angle?
- R: assign dataframe column values using external vectors
- Optimizing 2D vectors and arrays in C++
- Check if two vectors are equal
- Reorder a vector element to be second in R
- expected ';' at end of declaration /vector /c++
- Accesing elements from Vec using Vec of indices
- Initializing a two-dimensional std::vector
- On a mac, bash scripting shows uncertain behaviour w associative array (dictionaries)
- Is there a way to further optimize this code?
- Fill elements of a vector randomly depending on constraints
- Is there an option to create a vector that contains functions with different return types?
- How to find a series of number in a vector using a loop for - R
- How to deal with different vector-dimensions for embeddings and search with pgvector?
- SceneKit physicBody angularVelocity as local "body fixed" angular rates
- R: Trouble subsetting a vector for use with trapz function (pracma package)
- i am writing a small interpreter in c++, but it returns me an error "vector subscript out of range"
- Redis Vector Radius Search
- Iterating C++ vector from the end to the beginning
- emplace_back and push_back give 'double free or corruption (fasttop)' error although copy and move constructor are defined
- Searching for multiple elements using std::find()
- Attempting to build Dynamic Array from scratch: Segmentation Fault when using delete[] in append function
- Angles between two n-dimensional vectors in Python
- leaflet tiles with protomaps pmtiles are not visibles (seems to be transparent)
- OpenAI Embeddings API: How to extract the embedding vector?
- Are there any existed API to split IEnumerable<T> to many Vector<T> in CSharp？
- How do I sort array of pairs based on the greater value in first or second