Topic Description
Transmission Door
The question is: give n triangles, and ask for their size. Solving
Find the intersection point of all the lines and then do the scan lines according to the horizontal axis of these intersections
Two adjacent scan lines must be in the middle of a number of trapezoidal, the area can be directly used (top + bottom) * High/2 calculation
But notice that one side of the triangle is parallel to the y-axis, and if the edge is the starting edge of the triangle, the answer between the current scan line and the next scan line should compute the edge, and if the edge is the end of the triangle, the answer between the current scan line and the next scan line should not be calculated for this edge . Code
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Cmath> using namespace std;
#define N-the const double eps=1e-12;
const double INF=1E9;
int dcmp (double x) {if (x<=eps&&x>=-eps) return 0;
Return (x>0) 1:-1;
struct Vector {double x,y;
Vector (double x=0,double y=0) {x=x,y=y; BOOL operator < (const Vector &a) const {return x<a.x| |
(X==A.X&&Y<A.Y);
} void Read () {scanf ("%lf%lf", &x,&y);}};
typedef Vector Point;
struct line {point p,q;
Line (Point P=point (0,0), point Q=point (0,0)) {p=p,q=q;
}
};
Vector operator + (vector a,vector b) {return vector (A.X+B.X,A.Y+B.Y);}
Vector operator-(vector a,vector b) {return vector (A.X-B.X,A.Y-B.Y);}
Vector operator * (vector a,double b) {return vector (a.x*b,a.y*b);}
int n,lsh;
Double ans;
Double lsh[n*n*10];
Point Seg[n];
Line Line[n][3]; Double Cross (Vector A,vector b) {return a.x*b.y-a.y*b.x.} bool Ins (point A,point b,point c,point D) {Vector v,w,u;
v=a-c,w=c-d,u=b-d;
if (dcmp (Cross (v,w)) ==dcmp (Cross (u,w)) return 0;
V=c-a,w=b-a,u=d-a;
if (dcmp (Cross (v,w)) ==dcmp (Cross (u,w)) return 0;
return 1;
Point GLI (Point p,vector v,point q,vector w) {Vector u=p-q;
Double T=cross (w,u)/cross (V,W);
return p+v*t;
Double Plus (double x) {int cnt=0;
for (int i=1;i<=n;++i) {if (dcmp (line[i][1].p.x-line[i][1].q.x) ==0&&dcmp (x==line[i][1].p.x))
Continue
Double Min=inf,max=-inf; for (int j=1;j<=3;++j) {if (x<line[i][j].p.x| |
x>line[i][j].q.x) continue;
if (dcmp (line[i][j].p.x-line[i][j].q.x) ==0) continue;
Point P=gli (Line[i][j].p,line[i][j].q-line[i][j].p,point (X,-inf), Vector (0,inf));
Min=min (MIN,P.Y), Max=max (MAX,P.Y);
} if (max-min>eps) seg[++cnt]=point (Min,max);Sort (seg+1,seg+cnt+1);
if (!cnt) return 0.0;
Double l=seg[1].x,r=seg[1].y,sum=0.0;
for (int i=2;i<=cnt;++i) {if (seg[i].x-r>eps) sum+=r-l,l=seg[i].x,r=seg[i].y;
else R=max (R,SEG[I].Y);
} sum+=r-l;
return sum;
Double minus (double x) {int cnt=0;
for (int i=1;i<=n;++i) {if (dcmp (line[i][2].p.x-line[i][2].q.x) ==0&&dcmp (x==line[i][2].p.x))
Continue
Double Min=inf,max=-inf; for (int j=1;j<=3;++j) {if (x<line[i][j].p.x| |
x>line[i][j].q.x) continue;
if (dcmp (line[i][j].p.x-line[i][j].q.x) ==0) continue;
Point P=gli (Line[i][j].p,line[i][j].q-line[i][j].p,point (X,-inf), Vector (0,inf));
Min=min (MIN,P.Y), Max=max (MAX,P.Y);
} if (max-min>eps) seg[++cnt]=point (Min,max);
Sort (seg+1,seg+cnt+1);
if (!cnt) return 0.0;
Double l=seg[1].x,r=seg[1].y,sum=0.0;
for (int i=2;i<=cnt;++i) {if (seg[i].x-r>eps) sum+=r-l,l=seg[i].x,r=seg[i].y;
else R=max (R,SEG[I].Y);
} sum+=r-l;
return sum;
int main () {scanf ("%d", &n); for (int i=1;i<=n;++i) {point a,b,c;
A.read (), B.read (), C.read ();
if (a.x>b.x) swap (a.x,b.x), swap (A.Y,B.Y);
if (b.x>c.x) swap (b.x,c.x), swap (B.Y,C.Y);
if (a.x>b.x) swap (a.x,b.x), swap (A.Y,B.Y);
lsh[++lsh]=a.x,lsh[++lsh]=b.x,lsh[++lsh]=c.x;
Line[i][1]=line (A,b), Line[i][2]=line (b,c); Line[i][3]=line (A,C);
for (int i=1;i<=n;++i) for (int j=1;j<=3;++j) for (int k=i+1;k<=n;++k) for (int l=1;l<=3;++l) {point a=line[i][j].p,b=line[i][j].q,c=line[k][l].p,d=line[
K][L].Q;
if (INS (a,b,c,d)) {point Q=gli (a,b-a,c,d-c);
lsh[++lsh]=q.x; }} Sort(lsh+1,lsh+lsh+1);
Lsh=unique (lsh+1,lsh+lsh+1)-lsh-1;
Double Last=0.0,now;
for (int i=1;i<=lsh;++i) {now=plus (lsh[i]);
if (i>1) ans+= (now+last) * (lsh[i]-lsh[i-1))/2.0;
Last=minus (Lsh[i]);
printf ("%.2lf\n", ans-eps); }