I am trying to implement line segment intesection based on CLR, but I felt like there's somethign wrong with my code. It does not give correct value
for the line (x1,y1,x2,y2)
4 6 4 9
and
4 5 6 7
Code: Select all
int isintersect(line l1, line l2){
int tmp, tmp2,tmp3,tmp4,flag=0;
points p, q;
if (max(l1.p1.x,l1.p2.x) >= min(l2.p1.x,l2.p2.x) &&
max(l2.p1.x,l2.p2.x) >= min(l1.p1.x,l1.p1.x) &&
max(l1.p1.y,l1.p2.y) >= min(l2.p1.y,l2.p2.y) &&
max(l2.p1.y,l2.p2.y) >= min(l1.p1.x,l1.p1.y))
flag = 1;
if (flag == 0)
return 0;
p.x = l2.p1.x - l1.p1.x;
p.y = l2.p1.y - l1.p1.y;
q.x = l1.p2.x - l1.p1.x;
q.y = l1.p2.y - l1.p1.y;
tmp = cross(p,q);
p.x = l2.p2.x - l1.p1.x;
p.y = l2.p2.y - l1.p1.y;
tmp2 = cross(p,q);
if (tmp == 0 || tmp2 == 0)
return 1;
if (tmp != tmp2)
return 1;
return 0;
}