poj 3675 Telescope(三角剖分求多边形和圆交面积)

Telescope
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2271 Accepted: 673

Description

Updog is watching a plane object with a telescope. The field of vision in the telescope can be described as a circle. The center is at the origin and the radius is R. The object can be seen as a simple polygon of N vertexes. Updog wants to know the area of the part of the object that can be seen in the telescope.

Input

The input will contain several test cases. For each case:
The first line contains only one real number R
The second line contains an integer N. The following N lines contain two real numbers xi and yi each, which describe the coordinates of a vertex. Two vertexes in adjacent lines are also adjacent on the polygon. 
Constraints: 3 ≤ N ≤50, 0.1 ≤ R ≤1000, -1000 ≤ xiyi ≤ 1000

Output

For each test case, output one real number on a separate line, which is the area of the part that can be seen. The result should be rounded to two decimal places.

Sample Input

10
3
0 20
10 0
-10 0

Sample Output

144.35

Source


题意:求一个圆心为原点半径为r的圆和一个多边形的交面积,保证多边形不退化不自交
题解:根据圆心将多边形划分为n部分的三角形和圆的交,然后分情况计算三角形和圆的交面积。。。
就是一堆解释几何的问题,做完能很好的熟悉关于圆的各种计算。。。不过很麻烦。。。很容易错。。。我做了近5个小时
而三角形和圆交的基本情况在这个博客很详细  http://hi.baidu.com/billdu/item/703ad4e15d819db52f140b0b

#include<stdio.h>
#include<algorithm>
#include<math.h>
#define eps 1e-8
using namespace std;
struct point{
    double x,y;
    point(){}
    point(double x_,double y_):x(x_),y(y_){}
}p[58],tp[2],origin;
double r,area;
int n;
double MIN(double x,double y){ return x<y?x:y; }
double MAX(double x,double y){ return x>y?x:y; }
double cross(point p1,point p2,point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}
double dot(point p1,point p2)
{
    return p1.x*p2.x+p1.y*p2.y;
}
double dis(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
struct point get_intersect()
{
    struct point temp=point(tp[0].x-tp[1].x,tp[0].y-tp[1].y);
    struct point vec=point(temp.y,-temp.x);
    struct point origin2=point(origin.x+vec.x,origin.y+vec.y);
    double a1=tp[0].y-tp[1].y;
    double b1=tp[1].x-tp[0].x;
    double c1=(tp[0].x*tp[1].y-tp[1].x*tp[0].y);
    double a2=origin.y-origin2.y;
    double b2=origin2.x-origin.x;
    double c2=(origin.x*origin2.y-origin2.x*origin.y);
    double tmd=a1*b2-a2*b1;
    return point((b1*c2-b2*c1)/tmd,(a2*c1-a1*c2)/tmd);
};
int on_line(point p0,point p1,point p2)
{
    if(p0.x>MAX(p1.x,p2.x)) return 0;
    if(p0.x<MIN(p1.x,p2.x)) return 0;
    if(p0.y>MAX(p1.y,p2.y)) return 0;
    if(p0.y<MIN(p1.y,p2.y)) return 0;
    return 1;
}
double get_area()
{
    double len0=dis(origin,tp[0]);
    double len1=dis(origin,tp[1]);
    double angle=acos(dot(tp[0],tp[1])/len0/len1);
    struct point inter=get_intersect();
    double disinter=dis(inter,origin);
    double sgn=cross(origin,tp[0],tp[1])<0?-1:1;
    double res=0;

    if(angle<eps||fabs(cross(origin,tp[0],tp[1]))<eps) return 0;
    else if(len0<r+eps&&len1<r+eps)
    {
        res=fabs(cross(origin,tp[0],tp[1]))/2.0;
    }
    else if(len0<r-eps||len1<r-eps)
    {
        if(len1<r-eps)
        {
            swap(tp[0],tp[1]);
            swap(len0,len1);
        }
        double dis01=dis(tp[0],tp[1]);
        struct point mov=point((tp[1].x-tp[0].x)/dis01,(tp[1].y-tp[0].y)/dis01);
        double len=sqrt(r*r-disinter*disinter);
        struct point interpoint;
        interpoint=point(inter.x+mov.x*len,inter.y+mov.y*len);
        double angle0=acos(dot(tp[1],interpoint)/len1/r);
        res=r*r*angle0/2.0;
        res+=fabs(cross(origin,interpoint,tp[0]))/2.0;
    }
    else
    {
        int flag=on_line(inter,tp[0],tp[1]);
        res=r*r*angle/2.0;
        if(flag&&disinter<r)
        {
            double tangle=2*acos(disinter/r);
            res-=r*r*tangle/2.0;
            res+=r*disinter*sin(tangle/2.0);
        }
    }

    return res*sgn;
}
void solve()
{
    area=0;
    for(int i=1;i<=n;i++)
    {
        tp[0]=p[i],tp[1]=p[i+1];
        area+=get_area();
    }
}
int main()
{
    //freopen("t.txt","r",stdin);
    origin=point(0,0);
    while(scanf("%lf%d",&r,&n)>0)
    {
        for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        p[n+1]=p[1];
        solve();
        printf("%.2lf\n",fabs(area));
    }

    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值