# 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 - segment_1;
double dy_1 = segment_1 - segment_1;
/ Second vector */
double dx_2 = segment_2 - segment_2;
double dy_2 = segment_1 - segment_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, segment_1) - Point(segment_1, segment_1);
Point vec_2 = Point(segment_2, segment_2) - Point(segment_2, segment_2);
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