Calculate the number of intersection times of a bunch of line segments, even if the intersection is overlapped.
For more information, see http://dev.gameres.com/program/abstract/geometry.htm?
Sample Input
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
Sample Output
1
3
C ++ code
# Include <iostream>
# Include <fstream>
# Include <algorithm>
# Include <string>
# Include <set>
// # Include <map>
# Include <queue>
# Include <utility>
# Include <iomanip>
# Include <stack>
# Include <list>
# Include <vector>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <cmath>
# Include <ctime>
# Include <ctype. h>
Using namespace std;
# Define L _ int64
Struct point {// point structure
Double x, y;
Point (double a = 0, double B = 0) {x = a, y = B ;}
};
Struct line {// line Segment Structure
Point s, e;
Line (point a, point B) {s = a, e = B ;}
Line (){}
} L [2, 105];
Double multi (point a, point B, point c) // cross product to determine the point-to-line relationship
{
Double x1, y1, x2, y2;
X1 = B. x-a. x;
Y1 = B. y-a. y;
X2 = c. x-B. x;
Y2 = c. y-B. y;
Return x1 * y2-x2 * y1;
}
Bool intersect (line a, line B) // determines whether the two line segments are intersecting.
{
If (max (a. s. x, a. e. x)> = min (B. s. x, B. e. x) & // fast Rejection Test
Max (B. s. x, B. e. x)> = min (a. s. x, a. e. x )&&
Max (a. s. y, a. e. y)> = min (B. s. y, B. e. y )&&
Max (B. s. y, B. e. y)> = min (a. s. y, a. e. y )&&
Multi (a. s, B. s, B. e) * multi (a. e, B. s, B. e) <= 0 & // cross-site test
Multi (B. s, a. s, a. e) * multi (B. e, a. s, a. e) <= 0)
Return true;
Return false;
}
Int main ()
{
Int n, I, res, j;
While (scanf ("% d", & n), n)
{
For (I = 0; I <n; I ++)
Scanf ("% lf", & l [I]. s. x, & l [I]. s. y, & l [I]. e. x, & l [I]. e. y );
Res = 0;
For (I = 0; I <n; I ++)
For (j = I + 1; j <n; j ++)
If (intersect (l [I], l [j])
Res ++;
Printf ("% d \ n", res );
}
Return 0;
}