hdu - 4159 - Regular Convex Polygon - 精度問題

題意:給你三個頂點,這三個點是一個正多邊形上的頂點,問這個正多邊形最少有多少個邊?

思路:三個點,三角形–>外接圓–>必定也是該凸多邊形的外接圓-
我們可以把三個點當做一個三角形放在它的外接圓上,然後求出每個角的度(即三角形分的三個弧度所對應的圓周角),設頂點數爲n,我們只需計算,三角形任意兩點所對應的圓心角是否是2PI/n的倍數就可以了、、、並且i很小,可以枚舉。

在計算圓心角是否是2PI/n的倍數的時候,有一個小技巧,具體看代碼(轉)


枚舉是哪個角符合2 * PI / n, 估計是卡精度的題。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;

#define eps 1e-6
#define PI acos(-1.0)

bool zero(double n){
    return n < eps && n > -eps;
}

double dis(double x1, double y1, double x2, double y2){
    return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}

double angle(double a, double b, double c){
    return acos((a * a + b * b - c * c) / (2 * a * b));
}

bool check(double n){//判斷n是否爲整數
    return zero(n - (int) (n + 0.5));
}

int main(){
    double x[3],y[3], a, b, c, A, B, C;
    while(scanf("%lf %lf", & x[0], & y[0])){
        scanf("%lf %lf",&x[1], &y[1]);
        scanf("%lf %lf",&x[2], &y[2]);

        a = dis(x[0], y[0], x[1], y[1]);
        b = dis(x[1], y[1], x[2], y[2]);
        c = dis(x[0], y[0], x[2], y[2]);

        A = angle(a, b, c) / PI;
        B = angle(b, c, a) / PI;
        C = angle(a, c, b) / PI;

        int i;
        for(i = 3; i <= 1000; i ++){
            if(check(A * i) && check( B * i) && check(C * i))
                break;
        }

        printf("%d\n", i);
    }
}


發佈了177 篇原創文章 · 獲贊 2 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章