//半平面交+多边形和圆的交的面积 #include <cstdio> #include <algorithm> #include <cmath> #include <iostream> using namespace std; const int N = 2050; const double eps = 1e-8; const double pi = acos(-1.0); int sign(double d){ return d < -eps ? -1 : (d > eps); } struct point{ double x, y; point(double _x=0, double _y=0) : x(_x), y(_y) {} bool operator== (point p){ return fabs(x-p.x) < eps && fabs(y-p.y) < eps; } void read(){ scanf("%lf%lf", &x, &y); } void set(double _x, double _y){ x = _x; y = _y; } }pque[N], po[N]; struct seg{ point st, ed; double ang; void calang(){ ang = atan2(ed.y-st.y, ed.x-st.x); } }sque[N], segs[N]; struct circle{ point c; double r; }; int n; double r; point cen, cherry; inline double xmul(point st1, point ed1, point st2, point ed2){ return (ed1.x - st1.x) * (ed2.y - st2.y) - (ed1.y - st1.y) * (ed2.x - st2.x); } point intersectPoint(point st1, point ed1, point st2, point ed2){ //求相交直线的交点 double t = xmul(st2, st1, st2, ed2) / ((ed1.y-st1.y) * (ed2.x-st2.x) - (ed1.x-st1.x) * (ed2.y-st2.y)); return point(st1.x+(ed1.x-st1.x)*t, st1.y+(ed1.y-st1.y)*t); } bool cmpseg(seg a, seg b){ // return a.ang < b.ang; return sign(a.ang - b.ang) < 0; } //半平面交,第个扳半平面为segs[i][0]->segs[i][1]的右侧,结果放在ps中 void halfPlaneInter(seg* segs, int sn, point* ps, int& pn){ int i, j, k, l, r; seg ts; for(i = 0; i < sn; i++){ segs[i].calang(); } sort(segs, segs+sn, cmpseg); for(k = i = 0; i < sn; i++){ for(j = i; j < sn && sign(segs[i].ang-segs[j].ang) == 0; j++) ; for(ts = segs[i], i++; i < j; i++){ if(sign(xmul(ts.st, ts.ed, ts.st, segs[i].st)) > 0){ ts = segs[i]; } } segs[k++] = ts; i = j-1; } sn = k; if(sn <= 0){ pn = 0; return; } if(sn <= 2){ segs[sn] = segs[0]; for(l = r = 0; r < sn; r++){ sque[r] = segs[r]; pque[r] = intersectPoint(segs[r].st, segs[r].ed, segs[r+1].st, segs[r+1].ed); } }else{ l = r = 0; sque[r++] = segs[0]; sque[r++] = segs[1]; pque[0] = intersectPoint(sque[0].st, sque[0].ed, sque[1].st, sque[1].ed); for(i = 2; i < sn; i++){ while(r-l >= 2 && sign(xmul(segs[i].st, segs[i].ed, segs[i].st, pque[r-2])) <= 0) r--; sque[r++] = segs[i]; pque[r-2] = intersectPoint(sque[r-2].st, sque[r-2].ed, sque[r-1].st, sque[r-1].ed); } //删除多余的 半平面 while(r-l >= 2){ bool flag = false; if(sign(xmul(sque[r-1].st, sque[r-1].ed, sque[r-1].st, pque[l])) <= 0){ flag = true; l++; } if(sign(xmul(sque[l].st, sque[l].ed, sque[l].st, pque[r-2])) <= 0){ flag = true; r--; } if(!flag) break; } //这里需要注意,最后的结果可能是一个无限平面. pque[r-1] = intersectPoint(sque[l].st, sque[l].ed, sque[r-1].st, sque[r-1].ed); } for(pn = 0, i = l; i < r; i++){ ps[pn++] = pque[i]; } } double xmul(point st, point ed1, point ed2){ return (ed1.x-st.x) * (ed2.y-st.y) - (ed1.y-st.y) * (ed2.x-st.x); } double dist(point p1, point p2){ return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0)); } double dmul(point st, point ed1, point ed2){ return (ed1.x-st.x) * (ed2.x-st.x) + (ed1.y-st.y) * (ed2.y-st.y); } point getRoot(point p, point st, point ed){ point ans; double u=((ed.x-st.x)*(ed.x-st.x)+(ed.y-st.y)*(ed.y-st.y)); u = ((ed.x-st.x)*(ed.x-p.x)+(ed.y-st.y)*(ed.y-p.y))/u; ans.x = u*st.x+(1-u)*ed.x; ans.y = u*st.y+(1-u)*ed.y; return ans; } //二维空间计算点到直线的距离 double disptoline(point p, point st, point ed){ return fabs(xmul(p, st, ed)) / dist(st, ed); } //二维计算点到线段的最短距离 double disptoseg(point p, point st, point ed){ double d1 = dist(p, st); double d2 = dist(p, ed); double d3 = dist(st, ed); if(fabs(d1+d2-d3) < eps) return 0; if(fabs(d1+d3-d2) < eps || fabs(d2+d3-d1) < eps) return min(d1, d2); if((d3*d3+d2*d2-d1*d1) < eps || (d3*d3+d1*d1-d2*d2) < eps){ return min(d1, d2); } return disptoline(p, st, ed); } //求点p1和p2对应的劣弧对应的扇形的面积 double getSectorArea(circle cir, point p1, point p2){ double ang = acos(dmul(cir.c, p1, p2)/(dist(cir.c, p1)*dist(cir.c, p2))); return cir.r*cir.r*fabs(ang)*0.5; } //求点p1和p2对应的劣弧对应的弓形的面积 double getBowArea(circle cir, point p1, point p2){ double ang = acos(dmul(cir.c, p1, p2)/(dist(cir.c, p1)*dist(cir.c, p2))); double ans = cir.r*cir.r*fabs(ang)*0.5; return ans - cir.r*cir.r*sin(ang)*0.5; } //求从st走到ed遇到的第一个圆上的点(st到ed上的点到cir.c的距离得递增,并起,st在圆内,ed在圆外) point getPoint(circle cir, point st, point ed){ double l, r, mid, teps = 1e-10; point k(ed.x-st.x, ed.y-st.y), tp; l = 0.; r = 1.; while(l <= r){ mid = (l+r)*0.5; tp.x = st.x+mid*k.x; tp.y = st.y+mid*k.y; if(dist(tp, cir.c) <= cir.r){ l = mid+teps; }else{ r = mid-teps; } } mid = l-teps; tp.x = st.x+mid*k.x; tp.y = st.y+mid*k.y; return tp; } double getArea(circle cir, point p1, point p2){ double ans = 0; int k1, k2; k1 = sign(dist(p1, cir.c)-cir.r); k2 = sign(dist(p2, cir.c)-cir.r); if(k1 <= 0 && k2 <= 0){ ans = fabs(xmul(cir.c, p1, p2))*0.5; }else if(k1 > 0 && k2 > 0){ ans = getSectorArea(cir, p1, p2); if(sign(disptoseg(cir.c, p1, p2) - cir.r) < 0){ point rp = getRoot(cir.c, p1, p2); p1 = getPoint(cir, rp, p1); p2 = getPoint(cir, rp, p2); ans -= getBowArea(cir, p1, p2); } }else{ if(k1 > 0){ swap(p1, p2); } point rp = getRoot(cir.c, p1, p2), p3; p3 = getPoint(cir, rp, p2); ans = getSectorArea(cir, p3, p2); ans += fabs(xmul(cir.c, p1, p3))*0.5; } return ans; } //返回圆和多边形交的面积 double area(point* ps, int n, circle cir){ if(n <= 0) return 0; ps[n] = ps[0]; int i; double ans, tmp; ans = 0; for(i = 1; i <= n; i++){ if(fabs(ps[i-1].x-ps[i].x) < eps && fabs(ps[i-1].y-ps[i].y) < eps) continue; tmp = getArea(cir, ps[i-1], ps[i]); if(sign(xmul(cir.c, ps[i-1], ps[i])) > 0){ ans += tmp; }else{ ans -= tmp; } } return fabs(ans); } bool input(){ scanf("%lf%d", &r, &n); int i; for(i = 0; i < n; i++){ segs[i].st.read(); segs[i].ed.read(); } cherry.read(); for(i = 0; i < n; i++){ if(sign(xmul(segs[i].st, segs[i].ed, segs[i].st, cherry)) < 0){ swap(segs[i].st, segs[i].ed); } } return true; } int ca = 0; void solve(){ int cnt; double len = r*2; segs[n].st.set(-len, -len); segs[n++].ed.set(len, -len); segs[n].st.set(len, -len); segs[n++].ed.set(len, len); segs[n].st.set(len, len); segs[n++].ed.set(-len, len); segs[n].st.set(-len, len); segs[n++].ed.set(-len, -len); halfPlaneInter(segs, n, po, cnt); circle cir; cir.c.set(0, 0); cir.r = r; double ans = fabs(area(po, cnt, cir)); ans /= r*r*pi; printf("Case %d: %.5lf%%\n", ++ca, ans*100); } int main(){ int t; scanf("%d", &t); while(t--){ input(); solve(); } return 0; }