Question meaning:
Returns N line segments to determine the logarithm of the intersection of these N line segments.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1086
Question Analysis:
This topic mainly writes a function to determine the intersection of line segments, and then judges each line segment. The time complexity is O (n * n ). For more information, see the code.
AC code:
/**
* Determine whether there is an intersection between the line AB and the CD:
* Both conditions are met: ('x' indicates the cross product)
* 1. Point C is located on both sides of AB. (vector (abxac) * (abxad) <= 0)
* 2. points A and B are on both sides of the CD, respectively. (vector (cdxca) * (cdxcb) <= 0)
* 3. vector (abxac) * (abxad) <0 indicates that the vector is on both sides of the straight line, and = 0 indicates that the vector is on the straight line.
*/
# Include <iostream>
# Include <cmath>
# Include <cstring>
# Include <cstdio>
# Include <algorithm>
Using namespace STD;
Typedef struct node {
Double X, Y;
} Node;
Node P [105], P1 [101];
Double Direction (node Pi, node PJ, node PK) {// calculate the cross Multiplication
Return (Pk. x-pi.x) * (PJ. y-pi.y)-(PJ. x-pi.x) * (Pk. y-pi.y );
}
Bool segments_x (node P1, node P2, node P3, node P4) {// determine whether the two line segments overlap
Double D1, D2, D3, D4;
D1 = direction (P3, P4, P1 );
D2 = direction (P3, P4, P2 );
D3 = direction (P1, P2, P3 );
D4 = direction (P1, P2, P4 );
If (D1 * D2 <= 0 & D3 * D4 <= 0) return true;
Return false;
}
Int main ()
{
Int N;
While (CIN> N & N ){
For (INT I = 1; I <= N; I ++ ){
Cin> P [I]. x> P [I]. Y> P1 [I]. x> P1 [I]. Y;
}
Int K = 0;
For (INT I = 1; I <= n-1; I ++ ){
For (Int J = I + 1; j <= N; j ++ ){
If (segments_x (P [I], P1 [I], p [J], P1 [J]) K ++;
}
}
Cout <k <Endl;
}
Return 0;
}
Hdu1086 (intersection of line segments)