判断两线段是否相交
根据叉乘判断。
1 inline double CrossProduct(node a, node b, node c){ 2 return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); 3 } 4 //Calculate the crossproduct 5 6 7 inline bool SegX(node p1, node p2, node p3, node p4){ 8 double d1 = CrossProduct(p3, p4, p1); 9 double d2 = CrossProduct(p3, p4, p2); 10 double d3 = CrossProduct(p1, p2, p3); 11 double d4 = CrossProduct(p1, p2, p4); 12 return (d1 * d2 <= 0 && d3 * d4 <= 0); 13 } 14 //Judge whether the line segments intersact