题意:给你三个顶点,这三个点是一个正多边形上的顶点,问这个正多边形最少有多少个边?
思路:三个点,三角形–>外接圆–>必定也是该凸多边形的外接圆-
我们可以把三个点当做一个三角形放在它的外接圆上,然后求出每个角的度(即三角形分的三个弧度所对应的圆周角),设顶点数为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);
}
}