ECNA-A- Abstract Art

题目描述

Arty has been an abstract artist since childhood, and his works have taken on many forms. His latest (and most pricey) creations are lovingly referred to as Abstract Art within the abstract art community (they’re not the most original bunch when it comes to loving nicknames). Here’s an example of one of Arty’s recent works:
ECNA-A-	Abstract Art

As you can see, Abstract Art is created by painting (possibly overlapping) polygons. When Arty paints one of his designs he always paints each polygon completely before moving on to the next one. 
The price of individual pieces of Arty’s Abstract Art varies greatly based on their aesthetic appeal, but collectors demand two pieces of information about each painting: 
1. the total amount of paint used, and
2. the total amount of canvas covered.
Note that the first value will be larger than the second whenever there is overlap between two or more polygons. Both of these values can be calculated from a list containing the vertices of all the polygons used in the painting, but Arty can’t waste his time on such plebeian pursuits — he has great art to produce! I guess it’s left up to you.

输入

The first line of input contains a single integer n (1 ≤ n ≤ 100) representing the number of polygons to be painted. Following this are n lines each describing a painted polygon. Each polygon description starts with an integer m (3 ≤ m ≤ 20) indicating the number of sides in the polygon, followed by m pairs of integers x y (0 ≤ x, y ≤ 1 000) specifying the coordinates of the vertices of the polygon in consecutive order. Polygons may be concave but no polygon will cross itself. No point on the canvas will be touched by more than two polygon border segments.

输出

Display both the total amount of paint used and the amount of canvas covered. Your answers must have a relative or absolute error of at most 10−6.

样例输入

3
8 7 10 7 17 10 20 17 20 20 17 20 10 17 7 10 7
4 0 0 0 8 8 8 8 0
4 3 3 3 13 13 13 13 3

样例输出

315.00000000 258.50000000
一堆多边形的面积的并
存个板子
#include <bits/stdc++.h>
using namespace std;
const int N=1e3+;
const double eps=1e-;
int m;
double ans1,ans2;
int sgn(double x)
{
if (fabs(x)<eps) return ;
return x<?-:;
}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y)
{
x=_x; y=_y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const
{
return x*b.y-y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x+y*b.y;
} };
struct Polygon
{
int n;
Point p[];
void input()
{
for (int i=;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
p[n]=p[];
}
double area()
{
double res=;
for (int i=;i<n;i++) res+=p[i]^p[(i+)%n];
return res/2.0;
}
Point& operator[](int idx)
{
return p[idx];
}
}v[];
double cross(Point o,Point a,Point b)
{
return (a-o)^(b-o);
}
double seg(Point o,Point a,Point b)
{
if (sgn(b.x-a.x)==) return (o.y-a.y)/(b.y-a.y);
return (o.x-a.x)/(b.x-a.x);
}
pair<double,int>s[N];
double PolygonUnion()
{
int M,c1,c2;
double s1,s2,ret=;
for (int i=;i<m;i++)
{
for (int ii=;ii<v[i].n;ii++)
{
M=;
s[M++]=make_pair(0.00,);
s[M++]=make_pair(1.00,);
for (int j=;j<m;j++) if(j!=i)
{
for (int jj=;jj<v[j].n;jj++)
{
c1=sgn(cross(v[i][ii],v[i][ii+],v[j][jj]));
c2=sgn(cross(v[i][ii],v[i][ii+],v[j][jj+]));
if (c1== && c2==)
{
if (((v[i][ii+]-v[i][ii])*(v[j][jj+]-v[j][jj]))> && i>j)
{
s[M++]=make_pair(seg(v[j][jj],v[i][ii],v[i][ii+]),);
s[M++]=make_pair(seg(v[j][jj+],v[i][ii],v[i][ii+]),-);
}
}
else
{
s1=cross(v[j][jj],v[j][jj+],v[i][ii]);
s2=cross(v[j][jj],v[j][jj+],v[i][ii+]);
if (c1>= && c2<) s[M++]=make_pair(s1/(s1-s2),);
else if (c1< && c2>=) s[M++]=make_pair(s1/(s1-s2),-);
}
}
}
sort(s,s+M);
// for (int i=0;i<M;i++) cout<<s[i].first<<' '<<s[i].second<<endl;
double pre=min(max(s[].first,0.0),1.0),now;
double sum=;
int cov=s[].second;
for (int j=;j<M;j++)
{
now=min(max(s[j].first,0.0),1.0);
if (!cov) sum+=now-pre;
cov+=s[j].second;
pre=now;
}
ret+=(v[i][ii]^v[i][ii+])*sum;
}
}
return ret/;
} int main()
{
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d",&v[i].n);
v[i].input();
double nows=v[i].area();
if (sgn(nows<))
{
reverse(v[i].p,v[i].p+v[i].n);
nows*=-;
v[i][v[i].n]=v[i][];
}
ans1+=nows;
}
// cout<<'*'<<endl;
ans2=PolygonUnion();
printf("%.8f %.8f\n",ans1,ans2);
return ;
}
 
上一篇:《剑指offer》第十一题(旋转数组的最小数字)


下一篇:docker创建mysql5.7.22并配置主从