閱讀592 返回首頁    go 阿裏雲 go 技術社區[雲棲]


POJ 1375 過一點求圓切線極角

題意:給出一點,幾個圓的圓心半徑,問兩條切線與x軸相交組成的區間,然後合並一下。

這題我用解析做,精度損失的太高wa,然後又用極角做,還是精度問題,不過好把數的值擴大一些加上了eps之後過了。

具體看代碼。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct seg
{
    double l,r;
};
double dis(double lx,double ly,double cx,double cy)
{
    return sqrt((lx-cx)*(lx-cx)+(ly-cy)*(ly-cy));
}
bool cmp(seg a,seg b)
{
    return a.l<b.l;
}
double getx(double angle,double lx,double ly)
{
    return lx-((ly)/tan(angle));
}
int main()
{
    int n,i;
    seg g[505];
    double eps=1e-8;
    double lx,ly,cx,cy,r;
    while(~scanf("%d",&n),n)
    {
        scanf("%lf%lf",&lx,&ly);
        for(i=0; i<n; i++)
        {
            scanf("%lf%lf%lf",&cx,&cy,&r);
            double a0=atan2(ly-cy,lx-cx),a1=asin(r/dis(lx,ly,cx,cy));
            g[i].l=getx(a0-a1,lx,ly),g[i].r=getx(a0+a1,lx,ly);
            if(g[i].l>0)
                g[i].l+=eps;
            else
                g[i].l-=eps;
            if(g[i].r>0)
                g[i].r+=eps;
            else
                g[i].r-=eps;
            if(g[i].l>g[i].r)swap(g[i].l,g[i].r);
        }
        sort(g,g+n,cmp);
        i=0;
        while(i<n)
        {
            cx=g[i].l;
            cy=g[i++].r;
            while(i<n&&g[i].l<=cy)cy=max(cy,g[i++].r);
            printf("%.2f %.2f\n",cx,cy);
        }
        puts("");
    }
    return 0;
}


最後更新:2017-04-03 16:59:42

  上一篇:go cpu和內存的關係
  下一篇:go 【進程線程與同步】5.4 System.Threading.Interlocked 為多個線程共享的變量提供原子操作