I need to create a function that returns whether two lines are collinear or not. I found some solutions for this problem in 2D, such as the one in Algorithm for finding the segment overlapping two collinear segments but I didn't find one algorythm that allows to solve the same problem in 3D.
Asked
Active
Viewed 1,328 times
-6
-
2Please don't do tag spam, please tag correct language. Also post your attempts on this. – Pradeep Simha Jul 01 '16 at 13:01
-
I did no attempts. I want to find some code that does this, as I'me sure it has been done already, but I couldn't find it. – João Ferreira Jul 01 '16 at 13:06
-
What is the 2D solution? Colinnear is defined as a straight line which contain the same 3 or more points. The 2D and 3D solution should be the same. – jdweng Jul 01 '16 at 13:07
-
This is one possible solution for the 2D case: http://stackoverflow.com/questions/22456517/algorithm-for-finding-the-segment-overlapping-two-collinear-segments – João Ferreira Jul 01 '16 at 13:12
-
I think I may do it by following this link: http://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment Thanks – João Ferreira Jul 01 '16 at 13:17
-
"_Find the distance of point P from both the line end points A, B. If AB = AP + PB, then P lies on the line segment AB."_ From here: http://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment – Khalil Khalaf Jul 01 '16 at 13:22
-
Thank you, that solves it. In order to get a solution that is valid wherever points are chose, the code has to be something like this: AB = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1)); AP = sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1)); PB = sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y)+(z2-z)*(z2-z)); if (AB == AP + PB || AP == AB + PB || PB = AB + AP) return true; – João Ferreira Jul 04 '16 at 11:38
1 Answers
0
Two lines are collinear if scalar multiplication of two vectors equals absolute value of a multiplication their length (it works in 3D). Simply write a method that calculate scalar multiplication
private double scalarMultiply(Vector L1, Vector L2)
{
return L1.X()*L2.X() + L1.Y()*L2.Y() + L1.Z()*L2.Z();
}
and length of vectors
vectorMod = System.Math.Sqrt(x * x + y * y + z * z);
then you know what to do))

Igor Filimonov
- 16
- 2
-
Thank you, but since I'm using the vectors, am I not only making sure that the lines are parallel, and that depending on the position of the lines they can be collinear or not? – João Ferreira Jul 04 '16 at 09:42
-