判断两条线段是否相交的方法
1 struct pos
2 {
3 double x ;
4 double y ;
5 };
6 struct line
7 {
8 pos st ;
9 pos end ;
10 };
11 int n ;
12
13
14 double Multiply(pos p1, pos p2, pos p0)
15 {
16 return ( (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y) );
17 }
18
19 bool iscross(line L1, line L2)
20 {
21 return( (max(L1.st.x, L1.end.x) >= min(L2.st.x, L2.end.x)) &&
22 (max(L2.st.x, L2.end.x) >= min(L1.st.x, L1.end.x)) &&
23 (max(L1.st.y, L1.end.y) >= min(L2.st.y, L2.end.y)) &&
24 (max(L2.st.y, L2.end.y) >= min(L1.st.y, L1.end.y)) &&
25 (Multiply(L2.st, L1.end, L1.st) * Multiply(L1.end, L2.end, L1.st) >= 0) &&
26 (Multiply(L1.st, L2.end, L2.st) * Multiply(L2.end, L1.end, L2.st) >= 0)
27 );
28 }
29
30
2 {
3 double x ;
4 double y ;
5 };
6 struct line
7 {
8 pos st ;
9 pos end ;
10 };
11 int n ;
12
13
14 double Multiply(pos p1, pos p2, pos p0)
15 {
16 return ( (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y) );
17 }
18
19 bool iscross(line L1, line L2)
20 {
21 return( (max(L1.st.x, L1.end.x) >= min(L2.st.x, L2.end.x)) &&
22 (max(L2.st.x, L2.end.x) >= min(L1.st.x, L1.end.x)) &&
23 (max(L1.st.y, L1.end.y) >= min(L2.st.y, L2.end.y)) &&
24 (max(L2.st.y, L2.end.y) >= min(L1.st.y, L1.end.y)) &&
25 (Multiply(L2.st, L1.end, L1.st) * Multiply(L1.end, L2.end, L1.st) >= 0) &&
26 (Multiply(L1.st, L2.end, L2.st) * Multiply(L2.end, L1.end, L2.st) >= 0)
27 );
28 }
29
30