hdu2892-area 求园和多边形的相交面积模板题

来源:互联网 发布:名古屋大学研究生知乎 编辑:程序博客网 时间:2024/06/06 08:43

area

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 884    Accepted Submission(s): 355


Problem Description
小白最近被空军特招为飞行员,参与一项实战演习。演习的内容是轰炸某个岛屿。。。
作为一名优秀的飞行员,任务是必须要完成的,当然,凭借小白出色的操作,顺利地将炸弹投到了岛上某个位置,可是长官更关心的是,小白投掷的炸弹到底摧毁了岛上多大的区域?
岛是一个不规则的多边形,而炸弹的爆炸半径为R。
小白只知道自己在(x,y,h)的空间坐标处以(x1,y1,0)的速度水平飞行时投下的炸弹,请你计算出小白所摧毁的岛屿的面积有多大. 重力加速度G = 10.
 

Input
首先输入三个数代表小白投弹的坐标(x,y,h);
然后输入两个数代表飞机当前的速度(x1, y1);
接着输入炸弹的爆炸半径R;
再输入一个数n,代表岛屿由n个点组成;
最后输入n行,每行输入一个(x',y')坐标,代表岛屿的顶点(按顺势针或者逆时针给出)。(3<= n < 100000)
 

Output
输出一个两位小数,表示实际轰炸到的岛屿的面积。
 

Sample Input
0 0 2000100 0100 41900 1002000 1002000 -1001900 -100
 

Sample Output
15707.96
圆和多边形相交的面积直接相当于圆和三角形相交处理即可
直接套用模板即可:
代码如下:
#include <iostream>  #include <cstdio>  #include <string>  #include <cmath>  #include <iomanip>  #include <ctime>  #include <climits>  #include <cstdlib>  #include <cstring>  #include <algorithm>  #include <queue>  #include <vector>  #include <set>  #include <map>  using namespace std;  typedef unsigned int UI;  typedef long long LL;  typedef unsigned long long ULL;  typedef long double LD;  const double pi = acos(-1.0);  const double e = exp(1.0);  const double eps = 1e-8;  const int maxn = 100005;  double x, y, h;  double vx, vy;  double R;  int n;  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];    double xmult(point & p1, point & p2, point & p0);  double distancex(point & p1, point & p2);  point intersection(point u1, point u2, point v1, point v2);  void intersection_line_circle(point c, double r, point l1, point l2, point & p1, point & p2);  point ptoseg(point p, point l1, point l2);  double distp(point & a, point & b);  double Direct_Triangle_Circle_Area(point a, point b, point o, double r);    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 ptoseg(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 = ptoseg(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()  {      ios::sync_with_stdio(false);      while (scanf("%lf%lf%lf", &x, &y, &h) != EOF)      {          scanf("%lf%lf", &vx, &vy);          scanf("%lf", &R);          scanf("%d", &n);          x += vx*sqrt(h/5.0);          y += vy*sqrt(h/5.0);          point temp = point(x, y);          double sum = 0;          for (int i=0; i<n; i++)              scanf("%lf%lf", &area[i].x, &area[i].y);          for (int i=0; i<n-1; i++)              sum += Direct_Triangle_Circle_Area(area[i], area[i+1], temp, R);          sum += Direct_Triangle_Circle_Area(area[n-1], area[0], temp, R);          printf("%.2f\n", fabs(sum));      }      return 0;  }    
点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=2892