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:
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+10;
const double eps=1e-8;
int m;
double ans1,ans2;
int sgn(double x)
{
    if (fabs(x)<eps) return 0;
    return x<0?-1:1;
}
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[50];
    void input()
    {
        for (int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        p[n]=p[0];
    }
    double area()
    {
        double res=0;
        for (int i=0;i<n;i++) res+=p[i]^p[(i+1)%n];
        return res/2.0;
    }
    Point& operator[](int idx)
    {
        return p[idx];
    }
}v[105];
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)==0) 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=0;
    for (int i=0;i<m;i++)
    {
        for (int ii=0;ii<v[i].n;ii++)
        {
            M=0;
            s[M++]=make_pair(0.00,0);
            s[M++]=make_pair(1.00,0);
            for (int j=0;j<m;j++) if(j!=i)
            {
                for (int jj=0;jj<v[j].n;jj++)
                {
                    c1=sgn(cross(v[i][ii],v[i][ii+1],v[j][jj]));
                    c2=sgn(cross(v[i][ii],v[i][ii+1],v[j][jj+1]));
                    if (c1==0 && c2==0)
                    {
                        if (((v[i][ii+1]-v[i][ii])*(v[j][jj+1]-v[j][jj]))>0 && i>j)
                        {
                            s[M++]=make_pair(seg(v[j][jj],v[i][ii],v[i][ii+1]),1);
                            s[M++]=make_pair(seg(v[j][jj+1],v[i][ii],v[i][ii+1]),-1);
                        }
                    }
                    else
                    {
                        s1=cross(v[j][jj],v[j][jj+1],v[i][ii]);
                        s2=cross(v[j][jj],v[j][jj+1],v[i][ii+1]);
                        if (c1>=0 && c2<0) s[M++]=make_pair(s1/(s1-s2),1);
                        else if (c1<0 && c2>=0) s[M++]=make_pair(s1/(s1-s2),-1);
                    }
                }
            }
            sort(s,s+M);
           // for (int i=0;i<M;i++) cout<<s[i].first<<' '<<s[i].second<<endl;
            double pre=min(max(s[0].first,0.0),1.0),now;
            double sum=0;
            int cov=s[0].second;
            for (int j=1;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+1])*sum;
        }
    }
    return ret/2;
}

int main()
{
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d",&v[i].n);
        v[i].input();
        double nows=v[i].area();
        if (sgn(nows<0))
        {
            reverse(v[i].p,v[i].p+v[i].n);
            nows*=-1;
            v[i][v[i].n]=v[i][0];
        }
        ans1+=nows;
    }
   // cout<<'*'<<endl;
    ans2=PolygonUnion();
    printf("%.8f %.8f\n",ans1,ans2);
    return 0;
}
View Code

 

 
posted @ 2018-09-05 21:04  特特w  阅读(349)  评论(1编辑  收藏  举报