Line segment intersection code issues.

So I have tried two methods that should both work with line segments to determine if they are intersecting. The problem is on occasion I get a random false positive, or false negatives.

The commented code was my first attempt the rest of the code was my second attempt.

Found the logic at these sites:

homework - How do you detect where two line segments intersect? - Stack Overflow

Intersection point of two lines

Code:

`bool C_Line::Lines_Intersect(C_Line l)`

{

/*

C_Point r = p1 - p;

C_Point q = l.Get_Point1();

C_Point s = l.Get_Point2() - l.Get_Point1();

float RxS = r.Cross_Product(s);

if(RxS == 0)return false;

float QPxR = (q-p).Cross_Product(r);

if(QPxR == 0)return false;

float t = (q-p).Cross_Product(s)/RxS;

float u = QPxR/RxS;

if(t <= 1 && t >=0 && u <= 1 && u >= 0)return true;

else return false;

*/

C_Point A = p;

C_Point B = p1;

C_Point C = l.Get_Point1();

C_Point D = l.Get_Point2();

int x1 = A.Get_X();

int x2 = B.Get_X();

int x3 = C.Get_X();

int x4 = D.Get_X();

int y1 = A.Get_Y();

int y2 = B.Get_Y();

int y3 = C.Get_Y();

int y4 = D.Get_Y();

float denom = ((y4 - y3)*(x2 - x1)) - ((x4 - x3)*(y2 - y1));

if(denom == 0)return true;

float ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3)) / denom;

float ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3)) / denom;

if(ua <= 1 && ua >= 0 && ub <= 1 && ub >= 0)return true;

else return false;

}