Test instructions: give you a straight line and a number of circles, to find the length of the circle and the line is the proportion of the whole line
Problem-solving ideas: The ratio of the circle to the straight line is calculated by the method of determining the score point.
First step: (Determine whether the projection is in the x-axis or y-axis)
(1) When the line line.s (x, y), line.e (x, y) line.s.x and line.e.x are different, the line can be equated to the starting point of line.s.x, line.e.x;
(2) when not satisfied (1) (i.e. when line.s.x==line.e.x), when the line of LINE.S (x, y), line.e (x, y) Line.s.y and line.e.y are different. This line can be equated to the starting point for Line.s.x, line.e.x;
(3) When not satisfied (1) and (2) (i.e. LINE.S==LINE.E), this time the line is a point, no matter what the circle is not handed. The ratio of the circle to the whole line is 0;
Step Two: (Projecting the circle to the straight line obtained in the first step)
To find out the scope of the projection of the circle on the line;
Step Three:
Find out all the round and. Divides the circle by the length of the segment. The percentage of the intersection of the circle and the line segment is calculated;
#include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include < cmath>using namespace Std;const int MAX = 300;struct node{double x, y;}; struct Line{node s, e;}; Line line; Node P[max];d ouble 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)//infer if the circle has an intersection with a line {double x0 = o.x, y0=o.y;x11 = s.x, Y11 = s.y;double x 2 = 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);d Elta = Sqr (B)-4 * A * C;return delta > 0;} int CMP (Node A, Node B) {if (a.x < b.x) return 1;return 0;} int main () {int n, I, Cnt;int flag, Flagnum;double leng;while (scanf ("%d", &n) && n) {flagnum = 0;scanf ("%lf%lf% Lf%lf ", &line.s.x, &line.s.y, &line.e.x, &line.e.y); if (line.s.x!=line.e.x) {if (Line.s.x < line.e.x ) {tmp.x = LINE.S.X;TMP.Y = line.e.x;} else{tmp.x = Line.e.x;tmp.y = line.s.x;} Flag = 0;leng = Fabs (line.e.x-line.s.x);} else if (line.s.x==line.e.x && line.s.y!=line.e.y) {if (Line.s.y < line.e.y) {tmp.x = Line.s.y;tmp.y = Line.e.y; }else{tmp.x = Line.e.y;tmp.y = Line.s.y;} Flag = 1;leng = Fabs (LINE.E.Y-LINE.S.Y);} Elseflagnum = 1;cnt = 0;for (i=0; i<n; i++) {scanf ("%lf%lf%lf", &cir.x, &cir.y, &r); if (Flagnum) continue;if (Circle_cross_line (LINE.S, LINE.E, Cir, R)) {p[cnt].x = (-b-sqrt (delta))/(2.0*a);p [CNT].Y = (-b+sqrt (delta))/(2.0*a), if (flag==0) {p[cnt].x = p[cnt].x * DX + X11;P[CN T].Y = p[cnt].y * dx + x11;} else{p[cnt].x = p[cnt].x * dy + y11;p[cnt].y = p[cnt].y * dy + y11;} if (p[cnt].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.x) continue;if (p[cnt].x<tmp.x) p[cnt].x = tmp.x;if (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);d ouble sum = 0;tmp = P[0];for (i=1; i<cnt; i++) {if (p[i].x < TMP.Y) {if (P[i].y > 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 (to find the percentage of straight line to circle)