POJ 3675 Telescope

题意:给定一个不自交的多边形,要求和圆心在原点的圆的面积交.

思路:同POJ2986,是加强版

代码:

  1 #include<algorithm>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<iostream>
  6 struct Point{
  7     double x,y;
  8     Point(){}
  9     Point(double x0,double y0):x(x0),y(y0){}
 10 }p[200005],a[5],O;
 11 struct Line{
 12     Point s,e;
 13     Line(){}
 14     Line(Point s0,Point e0):s(s0),e(e0){}
 15 };
 16 int n;
 17 double R;
 18 const double eps=1e-8;
 19 const double Pi=acos(-1);
 20 double sgn(double x){
 21     if (x>eps) return 1.0;
 22     if (x<-eps) return -1.0;
 23     return 0;
 24 }
 25 Point operator *(Point p1,double x){
 26     return Point(p1.x*x,p1.y*x);
 27 }
 28 Point operator /(Point p1,double x){
 29     return Point(p1.x/x,p1.y/x);
 30 }
 31 double operator /(Point p1,Point p2){
 32     return p1.x*p2.x+p1.y*p2.y;
 33 }
 34 double operator *(Point p1,Point p2){
 35     return p1.x*p2.y-p1.y*p2.x;
 36 }
 37 Point operator +(Point p1,Point p2){
 38     return Point(p1.x+p2.x,p1.y+p2.y);
 39 }
 40 Point operator -(Point p1,Point p2){
 41     return Point(p1.x-p2.x,p1.y-p2.y);
 42 }
 43 double dis(Point p1){
 44     return sqrt(p1.x*p1.x+p1.y*p1.y);
 45 }
 46 double dis(Point p1,Point p2){
 47     return dis(Point(p1.x-p2.x,p1.y-p2.y));
 48 }
 49 double sqr(double x){
 50     return x*x;
 51 }
 52 double dist_line(Line p){
 53     double A,B,C,dist;
 54     A=p.s.y-p.e.y;
 55     B=p.s.x-p.e.x;
 56     C=p.s.x*p.e.y-p.s.y*p.e.x;
 57     dist=fabs(C)/sqrt(sqr(A)+sqr(B));
 58     return dist;
 59 }
 60 double get_cos(double a,double b,double c){
 61     return (b*b+c*c-a*a)/(2*b*c);
 62 }
 63 double get_angle(Point p1,Point p2){
 64     if (!sgn(dis(p1))||!sgn(dis(p2))) return 0.0;
 65     double A,B,C;
 66     A=dis(p1);
 67     B=dis(p2);
 68     C=dis(p1,p2);
 69     if (C<=eps) return 0.0;
 70     return acos(get_cos(C,A,B));
 71 }
 72 Point get_point(Point p){
 73     double T=sqr(p.x)+sqr(p.y);
 74     return Point(sgn(p.x)*sqrt(sqr(p.x)/T),sgn(p.y)*sqrt(sqr(p.y)/T));
 75 }
 76 double S(Point p1,Point p2,Point p3){
 77     return fabs((p2-p1)*(p3-p1))/2;
 78 }
 79 double work(Point p1,Point p2){
 80     double f=sgn(p1*p2),res=0;
 81     if (!sgn(f)||!sgn(dis(p1))||!sgn(dis(p2))) return 0.0;
 82     double l=dist_line(Line(p1,p2));
 83     double a=dis(p1);
 84     double b=dis(p2);
 85     double c=dis(p1,p2);
 86     if (a<=R&&b<=R){
 87         return fabs(p1*p2)/2.0*f;
 88     }
 89     if (a>=R&&b>=R&&l>=R){
 90         double ang=get_angle(p1,p2);
 91         return fabs((ang/(2.0))*(R*R))*f;
 92     }
 93     if (a>=R&&b>=R&&l<=R&&(get_cos(a,b,c)<=0||get_cos(b,a,c)<=0)){
 94         double ang=get_angle(p1,p2);
 95         return fabs((ang/(2.0))*(R*R))*f;
 96     }
 97     if (a>=R&&b>=R&&l<=R&&(get_cos(a,b,c)>0&&get_cos(b,a,c)>0)){
 98         double dist=dist_line(Line(p1,p2));
 99         double len=sqrt(sqr(R)-sqr(dist))*2.0;
100         double ang1=get_angle(p1,p2);
101         double cos2=get_cos(len,R,R);
102         res+=fabs(len*dist/2.0);
103         double ang2=ang1-acos(cos2);
104         res+=fabs((ang2/(2))*(R*R));
105         return res*f;
106     }
107     if ((a>=R&&b<R)||(a<R&&b>=R)){
108         if (b>a) std::swap(a,b),std::swap(p1,p2);
109         double T=sqr(p1.x-p2.x)+sqr(p1.y-p2.y);
110         Point u=Point(sgn(p1.x-p2.x)*sqrt(sqr(p1.x-p2.x)/T),sgn(p1.y-p2.y)*sqrt(sqr(p1.y-p2.y)/T));
111         double dist=dist_line(Line(p1,p2));
112         double len=sqrt(R*R-dist*dist);
113         double len2=sqrt(sqr(dis(p2))-sqr(dist));
114         if (fabs(dis(p2+u*len2)-dist)<=eps) len+=len2;
115         else len-=len2;
116         Point p=p2+u*len;
117         res+=S(O,p2,p);
118         double ang=get_angle(p1,p);
119         res+=fabs((ang/2.0)*R*R);
120         return res*f;
121     }
122     return 0;
123 }
124 int main(){
125     O=Point(0,0);
126     while (scanf("%lf",&R)!=EOF){
127         scanf("%d",&n);
128         for (int i=1;i<=n;i++)
129          scanf("%lf%lf",&p[i].x,&p[i].y);
130         p[n+1]=p[1];
131         double ans=0;
132         for (int i=1;i<=n;i++)
133          ans+=work(p[i],p[i+1]);
134         ans=fabs(ans);
135         printf("%.2f\n",ans);
136     }
137 }
时间: 2024-10-10 10:56:10

POJ 3675 Telescope的相关文章

POJ 3675 Telescope 简单多边形和圆的面积交

这道题得控制好精度,不然会贡献WA  QAQ 还是那个规则: int sgn(double x){ if(x > eps) return 1; else if(x < - eps) return -1; else return 0; } 思路:把简单多边形的每一个点和原点连线,就把这个多边形和圆的交变成了多个三角形与圆的交,根据有向面积的思路,加加减减就可以得到公共面积. 贴上代码了- #include <cstdio> #include <cstring> #incl

【转】[专题学习][计算几何]

原文地址:http://www.cnblogs.com/ch3656468/archive/2011/03/02/1969303.html 基本的叉积.点积和凸包等东西就不多说什么了,网上一搜一大堆,切一些题目基本熟悉了就差不多了. 一些基本的题目可以自己搜索,比如这个blog:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 接下来,研究了半平面交,思想方法看07年朱泽园的国家队论文,模板代码参考自我校大牛韬哥: http://www.o

计算几何题目分类

转载 一.基础题目 1.1 有固定算法的题目 A, 最近点对问题最近点对问题的算法基于扫描线算法.ZOJ 2107    Quoit Design    典型最近点对问题POJ    3714    Raid    变种最近点对问题 B,最小包围圆最小包围圆的算法是一种增量算法,期望是O(n).ZOJ    1450    Minimal Circle  HDU    3007    Buried memory C,旋转卡壳POJ 3608    Bridge Across Islands   

poj 3130 How I Mathematician Wonder What You Are!

How I Mathematician Wonder What You Are! Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3568   Accepted: 1906 Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big

POJ 1411 Calling Extraterrestrial Intelligence Again

Calling Extraterrestrial Intelligence Again Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10716   Accepted: 4210 Description A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto

POJ 3130 How I Mathematician Wonder What You Are!(半平面相交 多边形是否有核 模板)

题目链接:http://poj.org/problem?id=3130 Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest pa

穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753

下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November 16

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)