Cod sursa(job #116612)

Utilizator devilkindSavin Tiberiu devilkind Data 19 decembrie 2007 01:43:29
Problema Camera Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.9 kb
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

#define NMAX 2000
#define pb push_back
#define sz size()
#define INF 0x3f3f3f3f
#define eps 0.0001

struct punct{double x,y;} P,P1,P2;

punct pol[NMAX],a[NMAX],pol2[NMAX];

long int n,m,i,j,k,nrp,nrp2;
double ar,x1,y1,x2,y2;


inline double MINN(double a, double b)
{
if (a<b) return a;
return b;
}

inline double MAXX(double a, double b)
{
if (a<b) return b;
return a;
}

long int inapoi(long int cnt, long int nr)
{
if (cnt==1) return nr;
return cnt-1;
}

long int inainte(long int cnt, long int nr)
{
if (cnt==nr) return 1;
return cnt+1;
}

double det(punct p1, punct p2, punct p3)
{
return p1.x * p2.y + p2.x*p3.y + p3.x * p1.y - p1.x*p3.y - p3.x*p2.y - p2.x*p1.y;
}

double ab(double x)
{
if (x>eps) return x;
return -x;
}

void taie(punct p1, punct p2)
{
punct P1,P2,P3;
double a1,b1,c1,a2,b2,c2;
long int i;


a1=p1.y - p2.y;
b1=p2.x - p1.x;
c1=p1.x*p2.y - p2.x * p1.y;

nrp2=0;

for (i=1;i<=nrp;i++)
        {

        P2=pol[i];
        P1=pol[ inapoi(i,nrp) ];

        if ( ( (det(p1,p2,P1) > -eps)&&( det(p1,p2,P2) < eps) ) || ( (det(p1,p2,P1) < eps)&&( det(p1,p2,P2) > -eps) ) )
                        {
                        a2=P1.y - P2.y;
                        b2=P2.x - P1.x;
                        c2=P1.x * P2.y - P2.x * P1.y;
                        P3.x = (c2*b1 - c1 * b2) / (a1*b2 - a2*b1);
                        P3.y = (c2*a1 - c1 * a2) / (b1*a2 - b2*a1);

                        if ( (ab( pol2[nrp2].x - P3.x) > eps )||(ab( pol2[nrp2].y - P3.y) ) > eps )
                                        pol2[++nrp2]=P3;
                        }

        if (det( p1,p2,pol[i] ) > -eps)
                                if ( (ab( pol2[nrp2].x - pol[i].x) > eps )||(ab( pol2[nrp2].y - pol[i].y) ) > eps )
                                                        pol2[++nrp2]=pol[i];
        }

for (i=1;i<=nrp2;i++)
        pol[i]=pol2[i];
nrp=nrp2;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1673.in","r",stdin);
freopen("1673.out","w",stdout);
#endif

long int T;
for (scanf("%ld",&T);T;T--)
        {
        scanf("%ld",&n);

        nrp=0;

        x1=INF;
        y1=INF;
        x2=-INF;
        y2=-INF;
        for (i=1;i<=n;i++)
                {
                scanf("%lf %lf",&P.x,&P.y);
                a[i]=P;
                x1=MINN(x1,P.x);
                x2=MAXX(x2,P.x);
                y1=MINN(y1,P.y);
                y2=MAXX(y2,P.y);
                }

        reverse(a+1,a+n+1);

        P.x=x1;P.y=y1;pol[1]=P;
        P.x=x2;P.y=y1;pol[2]=P;
        P.x=x2;P.y=y2;pol[3]=P;
        P.x=x1;P.y=y2;pol[4]=P;
        nrp=4;

        for (i=1;i<=n;i++)
                taie(a[i],a[inainte(i,n)]);

        ar=0;
        for (i=1;i<=nrp;i++)
                {
                P1=pol[i];
                P2=pol[inainte(i,nrp)];

                ar=ar + det(pol[0],P1,P2)/2;
                }
        printf("%.2lf\n",ar);
        }

return 0;
}