# Two line segments collinearity

I've faced with an simple problem: I can't write a function which tells me whether two line segments are collinear or not. There is a piece of code:


double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
/* First vector /
double dx_1 = segment_1[2] - segment_1[0];
double dy_1 = segment_1[3] - segment_1[1];
/ Second vector */
double dx_2 = segment_2[2] - segment_2[0];
double dy_2 = segment_1[3] - segment_1[1];
//
return fabs(dx_1 * dy_2 - dx_2 * dy_1);
}


The segments come from HoughLinesP which detects segmenst perfectly on simple image manually generated. But the result of this running this code is 10836, which is obviusly not correct. I guess it is because of pixel's coordinates integer values, but I'm not sure. It is very weird for me. What am I doing wrong? Also I've tried another way to test collinearity which gave me correct result. Just consider the difference between dot and vectors lengths product


double isVectorsCollinear(Vec4i segment_1, Vec4i segment_2)
{
Point vec_1 = Point(segment_1[2], segment_1[3]) - Point(segment_1[0], segment_1[1]);
Point vec_2 = Point(segment_2[2], segment_2[3]) - Point(segment_2[0], segment_2[1]);
return fabs(norm(vec_1) * norm(vec_2) - vec_1.dot(vec_2));
}


I can use this code, but firstly, it is more computationally demanding (because of norm function calling), and secondly, I am really interested in why my first solution doesn't work?

edit retag close merge delete

Sort by » oldest newest most voted

Hi,

In the first example, I guess, you are using not the correct formula to check the collinearity of two lines. There exists several ways do to this. If you won't use norm function have a look at:

http://www.geeksforgeeks.org/check-if...

hope it helps!

more

Official site

GitHub

Wiki

Documentation