Question: I will give you a straight line and several circles, and find out the proportion of the length of the circle and the straight line to the whole line.
Solution: calculate the percentage of the Cross-circle between the circle and the straight line by using the method of determining the ratio of points.
Step 1: (determine whether the projection direction is the X axis or the Y axis)
(1) line. S (x, y), line. E (x, y) line. s. X and line. e. if X is different, this line can be equivalent to line at the starting point. s. x, line. e. x;
(2) When (1) is not met (that is, line. s. X = line. e. x), when the line of the line. S (x, y), line. E (x, y) line. s. Y and line. e. if y is different, this line can be equivalent to line at the starting point. s. x, line. e. x;
(3) When (1) and (2) are not met (that is, line. S = line. e) at this time, the straight line is a point, and any circle has no intersection with it. The proportion of the circle to the entire line is 0;
Step 2: (project the circle to the straight line obtained in step 1)
Returns the projection range of a circle on a straight line;
Step 3:
Calculate the sum of all circles, divide the sum of the circles by the length of the line segment, and calculate the percentage of the cross between the circle and the line segment to the line segment;
# include # include # include # include # include using namespace STD; const int max = 300; struct node {Double X, Y ;}; struct line {node S, e ;}; line; node P [Max]; Double A, B, C, Delta; double X, Y, R; double X11, y11, dx, Dy; node TMP, CIR; double sqr (Double X) {return x * X;} int circle_cross_line (node s, node E, node o, double r) // determines whether there is an intersection between a circle and a straight line {double X0 = O. x, Y0 = O. y; X11 = S. x, y11 = S. y; double X2 = E. x, y2 = E. y; dx = x2-X11, dy = Y2-y11; A = DX * dx + dy * dy; B = 2 * DX * (X11-X0) + 2 * dy * (y11-y0); C = sqr (x11-x0) + sqr (y11-y0)-sqr (r); Delta = sqr (B) -4 * a * C; return Delta> 0;} int CMP (node A, Node B) {if (. x P [CNT]. y) {double T = P [CNT]. x; P [CNT]. X = P [CNT]. y; P [CNT]. y = T;} If (P [CNT]. x> TMP. Y | P [CNT]. Y TMP. y) P [CNT]. y = TMP. y; CNT ++ ;}}if (flagnum | CNT = 0) printf ("0.00 \ n"); else {sort (p, p + CNT, CMP ); double sum = 0; TMP = P [0]; for (I = 1; I TMP. y) TMP. y = P [I]. y;} else {sum + = TMP. y-TMP. x; TMP = P [I];} sum + = TMP. y-TMP. x; printf ("%. 2f \ n ", sum/Leng * 100.0) ;}} return 0 ;}
Poj3819 coverage (percentage of the cross between a straight line and a circle in a straight line)