传送门
裸的扫描线。
枚举两条线段。判断是否有交点
然后按照交点横坐标排序
然后就是线段覆盖个问题了。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 333
#define ld long double
#define pa pair<ld,ld>
using namespace std;
const ld eps=1e-8,inf=1e10;
struct P{
ld x,y;
P(){}
P(ld _,ld __):x(_),y(__){}
void read(){
double _,__;
scanf("%lf%lf",&_,&__);
x=_,y=__;
}
friend bool operator <(P a,P b){
if (fabs(a.x-b.x)<eps) return a.y<b.y;
return a.x<b.x;
}
friend P operator +(P a,P b){
return P(a.x+b.x,a.y+b.y);
}
friend P operator -(P a,P b){
return P(a.x-b.x,a.y-b.y);
}
friend P operator *(ld a,P b){
return P(a*b.x,a*b.y);
}
friend ld operator *(P a,P b){
return a.x*b.x+a.y*b.y;
}
friend ld operator ^(P a,P b){
return a.x*b.y-b.x*a.y;
}
}a[N][3],p[N*N];
struct L{
P a,b;
L(){}
L(P _,P __){
a=_,b=__-_;
}
P operator [](int k){
return k?a+b:a;
}
friend bool cross(L x,L y){
return (x.b^(y[0]-x.a))*(x.b^(y[1]-x.a))<-eps&&(y.b^(x[0]-y.a))*(y.b^(x[1]-y.a))<-eps;
}
friend P getP(L x,L y){
P t=x.a-y.a;
ld tmp=(y.b^t)/(x.b^y.b);
return x.a+tmp*x.b;
}
}l[N][3],T;
pa f[N];
int n,m,cnt,tot;
ld ans,la,A,B,sum;
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
a[i][0].read(),a[i][1].read(),a[i][2].read();
p[++tot]=a[i][0],p[++tot]=a[i][1],p[++tot]=a[i][2];
sort(a[i],a[i]+3);
if (((a[i][2]-a[i][0])^(a[i][1]-a[i][0]))>eps){
l[i][0]=L(a[i][0],a[i][2]);
l[i][1]=L(a[i][2],a[i][1]);
l[i][2]=L(a[i][1],a[i][0]);
}
else{
l[i][0]=L(a[i][2],a[i][0]);
l[i][1]=L(a[i][1],a[i][2]);
l[i][2]=L(a[i][0],a[i][1]);
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<i;j++)
for (int x=0;x<3;x++)
for (int y=0;y<3;y++)
if (cross(l[i][x],l[j][y]))
p[++tot]=getP(l[i][x],l[j][y]);
sort(p+1,p+tot+1);
ans=0; la=p[1].x;
T=L(P(0,-inf),P(0,inf));
for (int i=2;i<=tot;i++){
T.a.x=(la+p[i].x)/2; cnt=0;
for (int j=1;j<=n;j++)
if (cross(l[j][0],T)){
A=getP(l[j][0],T).y;
if (cross(l[j][1],T))
B=getP(l[j][1],T).y;
else B=getP(l[j][2],T).y;
if (A>B) swap(A,B);
f[++cnt]=pa(A,B);
}
sort(f+1,f+cnt+1);
sum=0; A=-inf;
for (int j=1;j<=cnt;j++){
if (f[j].first>A){
sum+=f[j].second-f[j].first;
A=f[j].second;
}
else if (f[j].second>A){
sum+=f[j].second-A;
A=f[j].second;
}
}
ans+=(p[i].x-la)*sum;
la=p[i].x;
}
printf("%.2lf",(double)ans);
}