Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.
In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.
Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.
You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.
The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.
Output
Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.
题目出处:http://codeforces.com/problemset/problem/1/C
这是一道几何题,不需要用到什么算法,但是需要一定的几何数学知识才能够解决(好久没接触几何计算了。。顺便复习了下)。根据题目的描述,我们可以将问题提炼出来:根据给出的三点,确定包含这三点为顶点的最小面积的正多边形。首先我们要先确定三角形外接圆的半径,这个半径亦为正多边形每个顶点到中心的距离,其中S为面积,a、b、c为三边长:
void getAngle()
{
A = acos((b*b+c*c-a*a)/(2*b*c));
B = acos((a*a+c*c-b*b)/(2*a*c));
C = acos((b*b+a*a-c*c)/(2*b*a));
}
double getArea()
{
return 0.5*a*c*sin(B);
}
double getRadius()
{
return (a*b*c)/(4*S);
}
然后求得三边的圆心角的最大公约数,这里的eqs的值一定要设置对,,我试过设置成1e-5的话会出WA,略坑:
double gcd(double a,double b)
{
double eqs = 0.0001;
return a<eqs?b:gcd(fmod(b,a),a);
}
最后附上完整代码:
#include <bits/stdc++.h>
using namespace std;
double a,b,c;//edge
double A,B,C;//angle
double S;
double R;
const double pi = acos(-1);
void getAngle()
{
A = acos((b*b+c*c-a*a)/(2*b*c));
B = acos((a*a+c*c-b*b)/(2*a*c));
C = acos((b*b+a*a-c*c)/(2*b*a));
}
double getArea()
{
return 0.5*a*c*sin(B);
}
double getRadius()
{
return (a*b*c)/(4*S);
}
double gcd(double a,double b)
{
double eqs = 0.0001;
return a<eqs?b:gcd(fmod(b,a),a);
}
int main()
{
double x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
a = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b = sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
getAngle();
S = getArea();
R = getRadius();
double n = pi/gcd(gcd(A,C),B);
double result ;
result = n*0.5*R*sin(pi/n)*2*R*cos(pi/n);
cout<<fixed<<setprecision(8)<<result<<endl;
return 0;
}