HDOJ5130 多边形和圆相交面积 最简单的模板

来源:互联网 发布:网站维护页面源码 编辑:程序博客网 时间:2024/05/17 03:24

Signal Interference

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 2103 Accepted Submission(s): 1114
Special Judge

Problem Description
Two countries A-Land and B-Land are at war. The territory of A-Land is a simple polygon with no more than 500 vertices. For military use, A-Land constructed a radio tower (also written as A), and it’s so powerful that the whole country was under its signal. To interfere A-Land’s communication, B-Land decided to build another radio tower (also written as B). According to an accurate estimation, for any point P, if the euclidean distance between P and B is no more than k (0.2 ≤ k < 0.8) times of the distance between P and A, then point P is not able to receive clear signals from A, i.e. be interfered. Your task is to calculate the area in A-Land’s territory that are under B-Land’s interference.

Input
There are no more than 100 test cases in the input.

In each test case, firstly you are given a positive integer N indicating the amount of vertices on A-Land’s territory, and an above mentioned real number k, which is rounded to 4 digits after the decimal point.

Then N lines follow. Each line contains two integers x and y (|x|, |y| ≤ 1000), indicating a vertex’s coordinate on A’s territory, in counterclockwise or clockwise order.

The last two lines of a test case give radio tower A and B’s coordinates in the same form as vertexes’ coordinates. You can assume that A is not equal to B.

Output
For each test case, firstly output the case number, then output your answer in one line following the format shown in sample. Please note that there is a blank after the ‘:’.

Your solution will be accepted if its absolute error or relative error is no more than 10-6.

This problem is special judged.

Sample Input
4 0.5000
-1 -1
1 -1
1 1
-1 1
0 0
-1 0

Sample Output
Case 1: 0.2729710441

Source
2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)

Recommend
liuyiding | We have carefully selected several similar problems for you: 6181 6180 6179 6178 6177

根据题意中的k,可以列出一个方程,化简之后就是一个圆的方程,求出圆心和半径之后就可以套用多边形和圆相交面积的模板了。

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;const double PI = acos(-1.0);const double eps = 1e-12;const double e = exp(1.0);const int maxn = 1005;int i,j,n;double E,D,F,k;double xA,yA,xB,yB;double xr,yr,R;struct node{   double x,y;   node(){}   node (double xx, double yy) {x=xx; y=yy;}   node operator -(node s) {return node(x-s.x,y-s.y);}   node operator +(node s) {return node(x+s.x,y+s.y);}   double operator *(node s) {return x*s.x+y*s.y;}   double operator ^(node s) {return x*s.y-y*s.x;}}p[maxn];double max(double a, double b) {return a>b?a:b;}double min(double a, double b) {return a<b?a:b;}double len(node a) {return sqrt(a*a);}double dis(node a, node b) {return len(b-a);}double cross(node a, node b, node c) {return (b-a)^(c-a);}double dot(node a, node b, node c) {return (b-a)*(c-a);}bool judge(node a, node b, node c){    if (c.x>=min(a.x,b.x)        && c.x<=max(a.x,b.x)        && c.y>=min(a.y,b.y)        && c.y<=max(a.y,b.y))            return 1;    return 0;}double area(node b, node c, double r){     node a(0.0,0.0);     if (dis(b,c)<eps) {return 0.0;}     double h = fabs(cross(a,b,c))/dis(b,c);     if (dis(a,b)>r-eps && dis(a,c)>r-eps) {         double angle = acos(dot(a,b,c)/dis(a,b)/dis(a,c));         if (h>r-eps) {return 0.5*r*r*angle;}         else if (dot(b,a,c)>0 && dot(c,a,b)>0) {            double angle1 = 2*acos(h/r);            return 0.5*r*r*fabs(angle-angle1)+0.5*r*r*sin(angle1);         }         else return 0.5*r*r*angle;     }     else if (dis(a,b)<r+eps && dis(a,c)<r+eps) {return 0.5*fabs(cross(a,b,c));}     else {        if (dis(a,b)>dis(a,c)) swap(b,c);        if (fabs(dis(a,b))<eps) {return 0.0;}        if (dot(b,a,c)<eps) {            double angle1 = acos(h/dis(a,b));            double angle2 = acos(h/r) - angle1;            double angle3 = acos(h/dis(a,c)) - acos(h/r);            return 0.5*r*dis(a,b)*sin(angle2) + 0.5*r*r*angle3;        }        else {            double angle1 = acos(h/dis(a,b));            double angle2 = acos(h/r);            double angle3 = acos(h/dis(a,c)) - angle2;            return 0.5*r*dis(a,b)*sin(angle1+angle2) + 0.5*r*r*angle3;        }     }}void init(){    k = pow(k,2);    for (i=0; i<n; i++) scanf("%lf %lf",&p[i].x,&p[i].y);    scanf("%lf %lf %lf %lf",&xA,&yA,&xB,&yB);    p[n] = p[0];    D = (2.0*k*xA - 2.0*xB)/(1.0-k);    E = (2.0*k*yA - 2.0*yB)/(1.0-k);    F = 1.0*(pow(xB,2)+pow(yB,2)-k*(pow(xA,2)+pow(yA,2)))/(1.0-k);    xr = -D*0.5;    yr = -E*0.5;    R = sqrt(pow(D,2)+pow(E,2)-4.0*F)*0.5;    node O(xr,yr);    for (i=0; i<=n; i++) p[i] = p[i] - O;}int main(){    int cnt=0;    while (scanf("%d %lf",&n,&k)!=EOF) {          init();          double sum = 0,s;          node O(0.0,0.0);          for (i=0; i<n; i++) {             s = area(p[i],p[i+1],R);             if (cross(O,p[i],p[i+1])>0) sum += s;             else sum -= s;          }          //printf("%lf\n",k);          printf("Case %d: %.10lf\n",++cnt,fabs(sum));    }    return 0;}
阅读全文
1 0
原创粉丝点击