# 【CF528E】Triangles 3000（计算几何）

CF

## 题解

$S\Delta ABC=\frac{1}{2}(OA\times OB+OB\times OC+OC\times OA)$

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX 3030
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n;double ans;
struct Vect{double x,y;}O,g[MAX];
struct Line{double a,b,c,ang;}L[MAX];
bool operator<(Line a,Line b){return a.ang<b.ang;}
Vect Intersection(Line a,Line b)
{
if(fabs(a.a)>1e-9)
{
double y=(b.c*a.a-a.c*b.a)/(a.b*b.a-a.a*b.b);
double x=-(a.c+a.b*y)/a.a;
return (Vect){x,y};
}
else
{
double x=(b.c*a.b-a.c*b.b)/(a.a*b.b-a.b*b.a);
double y=-(a.c+a.a*x)/a.b;
return (Vect){x,y};
}
}
double Cross(Vect a,Vect b){return a.x*b.y-a.y*b.x;}
Vect operator-(Vect a,Vect b){return (Vect){a.x-b.x,a.y-b.y};}
Vect operator+(Vect a,Vect b){return (Vect){a.x+b.x,a.y+b.y};}
bool cmp(Vect a,Vect b){return Cross(a,b)>=0;}
int main()
{
for(int i=1;i<=n;++i)
{
double x,y;
if(fabs(L[i].b)>1e-7)x=1,y=-L[i].a/L[i].b;
else y=1,x=-L[i].b/L[i].a;
L[i].ang=atan2(y,x);
}
sort(&L[1],&L[n+1]);
/*
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
for(int k=j+1;k<=n;++k)
{
Vect g[3];
g[0]=Intersection(L[i],L[j]);
g[1]=Intersection(L[j],L[k]);
g[2]=Intersection(L[k],L[i]);
ans-=(Cross(g[0],g[1])+Cross(g[1],g[2])+Cross(g[2],g[0]));
}
ans/=1.0*n*(n-1)*(n-2)/3;
printf("%.10lf\n",ans);ans=0;
*/
for(int i=1;i<=n;++i)
{
Vect s=(Vect){0,0};
for(int j=i%n+1;j!=i;j=j%n+1)
{
Vect a=Intersection(L[i],L[j]);
ans+=Cross(s,a);s=s+a;
}
}
ans/=1.0*n*(n-1)*(n-2)/3;
printf("%.10lf\n",ans);
return 0;
}

posted @ 2019-07-10 14:41  小蒟蒻yyb  阅读(541)  评论(0编辑  收藏  举报