Ask Your Question

Two line segments collinearity

asked 2017-09-10 04:48:18 -0500

psi gravatar image

updated 2017-09-10 07:26:57 -0500

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) -;

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-12 08:06:36 -0500

VxW gravatar image


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:

hope it helps!

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-09-10 04:48:18 -0500

Seen: 716 times

Last updated: Sep 12 '17