【题目链接】:click here~~
【题目大意】:
Description
Input
Output
Sample Input
0 0 2000 100 0 100 4 1900 100 2000 100 2000 -100 1900 -100
Sample Output
15707.96
Source
由此,套入模板求出面积。
代码:
/* * 多边形和圆面积并 * Problem: HDU No.2892 * Running time: 62MS * Complier: G++ * Author: javaherongwei * Create Time: 15:34 2015/10/1 星期四 */ #include <math.h> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const double pi=acos(-1.0); const double e=exp(1.0); const double eps=1e-8; const int maxn=100005; double R,k; int n,m; struct point // 点或向量结构 { double x,y; point(double _x=0.0,double _y=0.0):x(_x),y(_y) {} point operator - (const point &p) { return point(x-p.x,y-p.y); } double sqrx() //向量的模 { return sqrt(x*x+y*y); } } area[maxn]; int dcmp(double x) { return (x>eps)-(x<-eps); } double xmult(point &p1,point &p2,point &p0)//叉积 { return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); } double distancex(point &p1,point &p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } point intersection(point u1,point u2,point v1,point v2) //两直线交点 { point ret = u1; double t = ((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x)); ret.x += (u2.x-u1.x)*t; ret.y += (u2.y-u1.y)*t; return ret; } void intersection_line_circle(point c, double r, point l1, point l2, point & p1, point & p2) //直线与圆相交 { point p = c; double t; p.x += l1.y-l2.y; p.y += l2.x-l1.x; p = intersection(p, c, l1, l2); t = sqrt(r*r-distancex(p, c)*distancex(p, c))/distancex(l1, l2); p1.x = p.x+(l2.x-l1.x)*t; p1.y = p.y+(l2.y-l1.y)*t; p2.x = p.x-(l2.x-l1.x)*t; p2.y = p.y-(l2.y-l1.y)*t; } point len_pot_seg(point p, point l1, point l2)//点到线段的最近距离 { point t = p; t.x += l1.y-l2.y; t.y += l2.x-l1.x; if (xmult(l1, t, p)*xmult(l2, t, p)>eps) return distancex(p, l1)<distancex(p, l2) ? l1 : l2; return intersection(p, t, l1, l2); } double distp(point & a, point & b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } double Direct_Triangle_Circle_Area(point a, point b, point o, double r) { double sign = 1.0; a = a-o; b = b-o; o = point(0.0, 0.0); if(fabs(xmult(a, b, o)) < eps) return 0.0; if(distp(a, o) > distp(b, o)) { swap(a, b); sign = -1.0; } if (distp(a, o) < r*r+eps) { if (distp(b, o) < r*r+eps) return xmult(a, b, o)/2.0*sign; point p1, p2; intersection_line_circle(o, r, a, b, p1, p2); if (distancex(p1, b) > distancex(p2, b)) swap(p1, p2); double ret1 = fabs(xmult(a, p1, o)); double ret2 = acos((p1.x*b.x+p1.y*b.y)/p1.sqrx()/b.sqrx())*r*r; double ret = (ret1+ret2)/2.0; if (xmult(a, b, o)<eps && sign>0.0 || xmult(a, b, o)>eps && sign<0.0) ret = -ret; return ret; } point ins = len_pot_seg(o, a, b); if(distp(o, ins)>r*r-eps) { double ret = acos((a.x*b.x+a.y*b.y)/a.sqrx()/b.sqrx())*r*r/2.0; if(xmult(a, b, o)<eps && sign>0.0 || xmult(a, b, o)>eps && sign<0.0) ret = -ret; return ret; } point p1, p2; intersection_line_circle(o, r, a, b, p1, p2); double cm = r/(distancex(o, a)-r); point m = point((o.x+cm*a.x)/(1+cm),(o.y+cm*a.y)/(1+cm)); double cn = r/(distancex(o, b)-r); point n = point((o.x+cn*b.x)/(1+cn),(o.y+cn*b.y)/(1+cn)); double ret1 = acos((m.x*n.x+m.y*n.y)/m.sqrx()/n.sqrx())*r*r; double ret2 = acos((p1.x*p2.x+p1.y*p2.y)/p1.sqrx()/p2.sqrx())*r*r-fabs(xmult(p1, p2, o)); double ret = (ret1-ret2)/2.0; if(xmult(a, b, o)<eps && sign>0.0||xmult(a, b, o)>eps && sign<0.0) ret=-ret; return ret; } int main() { int tot=1; double x,y,H; double x1,y1,R; while(cin>>x>>y>>H) { cin>>x1>>y1; cin>>R; cin>>n; for(int i=0; i<n; i++) scanf("%lf%lf", &area[i].x, &area[i].y); double V=sqrt(2*10*H); double t=V/10; double ceter_x0=x+x1*t; double ceter_y0=y+y1*t; point ceter_xy=point(ceter_x0,ceter_y0); double sum=0.0; for(int i=0; i<n-1; i++) sum += Direct_Triangle_Circle_Area(area[i], area[i+1], ceter_xy, R); sum += Direct_Triangle_Circle_Area(area[n-1], area[0], ceter_xy, R); printf("%.2f\n",fabs(sum)); } return 0; }