计算几何专项:UVa 10439

易知3个点连线的中垂线的交点即为正多边形的中心,同时正n边形上点与中心连线的夹角为2*pi/n的整数倍,因为正多边形最多只有200个顶点,所以暴力即可。

这道题精度不能弄得太高,我用1e-6连样例都过不了,改成1e-4就AC了。

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-4;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
struct point
{
    double x,y;
    point(double x=0,double y=0):x(x),y(y){}
};
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
point operator+(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator*(point a,double p){return point(a.x*p,a.y*p);}
double angle(point a){return atan2(a.y,a.x);}
double cross(point a,point b){return a.x*b.y-a.y*b.x;}
point getinter(point p,point v,point q,point w)
{
    point u=p-q;
    double t=cross(w,u)/cross(v,w);
    return p+v*t;
}
point rot(point a,double rad)
{
    return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
int main()
{
    freopen("in.txt","r",stdin);
    int T;
    cin>>T;
    while(T--)
    {
        point a,b,c;
        cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
        point ab=rot(a-b,0.5*acos(-1.0));
        point bc=rot(b-c,0.5*acos(-1.0));
        point o=getinter(a*0.5+b*0.5,ab,b*0.5+c*0.5,bc);
        double A=angle(a-o),B=angle(b-o),C=angle(c-o);
        int ans=0;
        for(int i=3;i<=200;i++)
        {
            double rad=2*acos(-1.0)/i;
            int f1=0,f2=0,f3=0;
            for(int j=1;j<=i;j++)
            {
                if(dcmp(j*rad-fabs(A-B))==0) f1=1;
                if(dcmp(j*rad-fabs(B-C))==0) f2=1;
                if(dcmp(j*rad-fabs(A-C))==0) f3=1;
                if(f1&&f2&&f3)
                {
                    ans=i;
                    break;
                }
            }
            if(ans) break;
        }
        cout<<ans<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值