// hdu1086 find an intersection point number n of segments. . .
//http://acm.hdu.edu.cn/showproblem.php?pid=1086
// The following is a tip translated:
// you N (1 <= N <= 100) a segment (segments), output a number of all intersection points. If M (M> 2) line segments intersect at the same point, calculation should be repeated.
// You can assume two segments do not intersect more than one. (Do not overlap)
// very general solution: through each two line segments to determine whether there intersection.
// determine whether two lines intersect approach: two simultaneous linear. Seek intersection coordinates. . .
// high school knowledge, do not remember, can be derived directly.
// linear equation general formula: (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2) = 0; original formula (A * x + B * y + C = 0)
// intersection coordinates x: (b1 * c2 - b2 * c1) / (a1 * b2 - a2 * b1), y: (a2 * c1 - a1 * c2) / (a1 * b2 - a2 * b1);
// If a1 * b2 - a2 * b1 == 0 indicates that there is no intersection (parallel)
1 #include<cstdio> 2 3 struct Line { 4 double x1, y1, x2, y2; 5 } line[105]; 6 7 int n; 8 9 bool intersect(int l1, int l2) { 10 double a1 = line[l1].y2 - line[l1].y1; 11 double b1 = line[l1].x1 - line[l1].x2; 12 double c1 = line[l1].x2*line[l1].y1 - line[l1].x1*line[l1].y2; 13 double a2 = line[l2].y2 - line[l2].y1; 14 double b2 = line[l2].x1 - line[l2].x2; 15 double c2 = line[l2].x2*line[l2].y1 - line[l2].x1*line[l2].y2; 16 if(a1*b2 - a2*b1 == 0) return false; 17 double x = (b1*c2 - b2*c1)/(a1*b2 - a2*b1); 18 double y = (a2*c1 - a1*c2)/(a1*b2 - a2*b1); 19 if((x >= line[l1].x1 && x <= line[l1].x2 || x <= line[l1].x1 && x >= line[l1].x2) 20 && (x >= line[l2].x1 && x <= line[l2].x2 || x <= line[l2].x1 && x >= line[l2].x2)) return true; 21 return false; 22 } 23 24 int main() { 25 while(scanf("%d", &n) == 1 && n) { 26 for(int i = 0; i != n; ++i) { 27 scanf("%lf%lf%lf%lf", &line[i].x1, &line[i].y1, &line[i].x2, &line[i].y2); 28 } 29 int ans = 0; 30 for(int i = 0; i != n-1; ++i) { 31 for(int j = i+1; j != n; ++j) { 32 if(intersect(i, j)) ans++; 33 } 34 } 35 printf("%d\n", ans); 36 } 37 return 0; 38 }