http://codeforces.com/contest/1/problem/C
第一次做计算几何相关问题,嘿嘿,对着别人的板子写的,但是学到了很多东西
题意:给你一个正多边形的三个顶点的坐标,问你这个可能的正多边形的最小面积是什么
思路:求给定三角型的外接圆,根据外接圆求出三个圆心角,再求出三个圆心角的最大公约数θ,2*pi/θ
就是正多边形中小三角形的个数,我们此时只需要求出小三角形的面积即可,注意精度不需太大,1e-2 --1e-4之间即可
学到的东西:
已知三角形三条边求面积:海伦公式:s = (p * (p-a) * (p-b) * (p-c)); p = (a+b+c)/2;
已知三角形三条边和面积求外接圆的圆心:r =abc/4s;
余弦定理求角度:a^2 = b^2+c^2-2bccos(A);
浮点数求最大公约数需要用到fmod(),用于浮点数的取模.
fgcd写法和gcd相似,不过因为有偏差eps,∴应该这样写:
double fgcd(double a, double b)
{
if(b <= eps) return a;
else return fgcd(b, fmod(a, b));
}
第一次写计算几何,发现这个东西认真写起来真的需要很专注,不然出个bug很难找,计算需要相当的精细,所以这就需要套模板了,以后多收集一些模板吧,真累.jpg
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-2;
double fgcd(double a, double b)
{
if(b <= eps) return a;
else return fgcd(b, fmod(a, b));
}
struct node
{
double x;
double y;
}a, b, c;
double dis(node a, node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main()
{
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
double ab, ac, bc;
ab = dis(a, b);
ac = dis(a, c);
bc = dis(b, c);
double p = (ab + ac + bc) / 2;
double s = sqrt(p*(p-ab)*(p-ac)*(p-bc));
double r = ab*ac*bc / (4*s);
double ang, ang1, ang2, ang3;
ang1 = acos(1 - ab*ab/(2*r*r));
ang2 = acos(1 - bc*bc/(2*r*r));
ang3 = 2*pi - ang1 - ang2;
ang = fgcd(fgcd(ang1, ang2), fgcd(ang2, ang3));
double ans = r * r * sin(ang) * pi / ang;
printf("%.6f\n", ans);
return 0;
}
然而这个代码被卡在了第47枚数据,超时无法输出,不知道为什么的话只能换种思路了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define ll long long
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-2;
double fgcd(double a, double b)
{
if(b <= eps) return a;
else return fgcd(b, fmod(a, b));
}
struct node
{
double x;
double y;
}a, b, c;
double dis(node a, node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main()
{
scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
double ab, ac, bc, A, B, C, D;
ab = dis(a, b);
ac = dis(a, c);
bc = dis(b, c);
A = acos((ac*ac+ab*ab-bc*bc)/(2*ac*ab)) * 2;
B = acos((bc*bc+ab*ab-ac*ac)/(2*bc*ab)) * 2;
C = acos((ac*ac+bc*bc-ab*ab)/(2*ac*bc)) * 2;
D = fgcd(fgcd(A, B), fgcd(C, A));
double r = bc / (2*sin(A/2));
double ans = r * r / 2.0 * sin(D) * pi / D * 2.0;
printf("%.6f\n", ans);
return 0;
}