Cod sursa(job #1279913)

Utilizator danyro364Savu Ioan Daniel danyro364 Data 1 decembrie 2014 02:07:17
Problema Camera Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.34 kb
//#include <iostream>
#include <algorithm>
#include <fstream>
#include <iomanip>

#define inf 100001
#define nmax 2001
#define eps 1e-6
using namespace std;
//FILE *f=fopen("arie.in","r"),*g=fopen("arie.out","w");
ifstream f("camera.in"); ofstream g("camera.out");
struct punct{
    double x, y;
}p[nmax],q[nmax],temp[nmax];
int n,m;

double arie(punct v[], int lg)
{
   int i,j; double s=0,x;
    for(i=1;i<=lg;i++)
    {
        if(i==lg) j=1; else j=i+1;
        x=(double)v[i].x*v[j].y-v[j].x*v[i].y;
        s+=x;
    }
    return s;
}
void aflacoef(punct p, punct q, double &a, double &b, double &c)
{
    a=q.y-p.y;
    b=-q.x+p.x;
    c=q.x*p.y-p.x*q.y;
}
int aflasemn(double a, double b, double c, punct p)
{
    double s=a*p.x+b*p.y+c;
    if(s<-eps)
        return -1;
    if(s>eps)
        return 1;
    return 0;
}
punct intersectie(double a1, double b1, double c1, punct p, punct q,punct pp)
{
    double a2,b2,c2;
    punct pu;

    aflacoef(p,q,a2,b2,c2);
    double det = a1*b2-a2*b1;
    pu.x = (b1*c2-b2*c1)/det;
    pu.y = (a2*c1-a1*c2)/det;
    return pu;
}
int main()
{
    int i,j,k,t,semn[nmax],lt,lg,semntemp; double a,b,c;
    f>>n;
    for(i=1; i<=n; i++)
        f>>p[i].x>>p[i].y;
    if(arie(p,n)<0)
        reverse(p+1,p+1+n);
    f>>m;
    q[1].x=-inf; q[1].y=-inf;
    q[2].x=inf; q[2].y=-inf;
    q[3].x=inf; q[3].y=inf;
    q[4].x=-inf; q[4].y=inf;
    m=4;
    lg=m;
    for(i=1; i<=n; i++)
    {
        if(i==n) j=1; else j=i+1;
        aflacoef(p[i],p[j],a,b,c);
        for(k=1; k<=lg; k++)
            semn[k]=aflasemn(a,b,c,q[k]);
        lt=0;
        for(k=1; k<=lg; k++)
        {
            if(k==lg) t=1; else t=k+1;
            if(semn[k]<=0&&semn[t]<=0)
                temp[++lt]=q[k];
            else if(semn[k]<=0&&semn[t]>0)
                temp[++lt]=q[k],temp[++lt]=intersectie(a,b,c,q[k],q[t],p[i]);
            else if(semn[k]>0&&semn[t]<=0)
                temp[++lt]=intersectie(a,b,c,q[k],q[t],p[i]);
        }
        lg=lt;
        for(k=1; k<=lt; k++)
            q[k]=temp[k];
    }
    //fclose(f);
    /*for(i=1;i<=lg;i++)
        cout<<q[i].x<<" "<<q[i].y<<endl;*/
    g<<fixed<<setprecision(2)<<(double)arie(q,lg)/2.0;
    //fprintf(g,"%.3f",(double)arie(q,lg)/2.0);
    //fclose(g);
    f.close(); g.close();
    return 0;
}