Cod sursa(job #75493)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 2 august 2007 13:51:47
Problema Reuniune Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.47 kb
# include<stdio.h>

double p1,p2,s;
typedef struct MUCHIE {double x,y1,y2;};
typedef struct DREPTUNGHI {double x1,y1,x2,y2;};
DREPTUNGHI drept[4];
MUCHIE muc[5];
long int lenmuc;
typedef struct INTERVAL {double a,b;};

void citire()
{
FILE *f=fopen("reuniune.in","r");
int i;
for (i=1;i<=3;i++)
fscanf(f,"%f%f%f%f",&drept[i].x1,&drept[i].y1,&drept[i].x2,&drept[i].y2);
fclose(f);
}

void pune_muchiile(long int &l)
{
muc[1].x=drept[1].x1;muc[1].y1=drept[1].y1;muc[1].y2=drept[1].y2;
muc[2].x=drept[2].x1;muc[2].y1=drept[2].y1;muc[2].y2=drept[2].y2;
muc[3].x=drept[3].x1;muc[3].y1=drept[3].y1;muc[3].y2=drept[3].y2;
l=3;
}

void pune_muchiile_2(long int &l)
{
muc[1].x=drept[1].y2;muc[1].y1=drept[1].x1;muc[1].y2=drept[1].x2;
muc[2].x=drept[2].y2;muc[2].y1=drept[2].x1;muc[2].y2=drept[2].x2;
muc[3].x=drept[3].y2;muc[3].y1=drept[3].x1;muc[3].y2=drept[3].x2;
l=3;
}

void intersecteaza(INTERVAL a, INTERVAL b, INTERVAL &c)
{
if (a.a>b.a) c.a=a.a; else c.a=b.a;
if (a.b<b.b) c.b=a.b; else c.b=b.b;
if (c.a>=c.b) c.a=c.b=0;
}

double length(INTERVAL a) {return a.b-a.a;}
double aria(DREPTUNGHI a) {return (a.x2-a.x1)*(a.y2-a.y1);}

void baleiaza(double &p)
{
INTERVAL a,b,c,iab,iac,ibc,iabc;
a.a=muc[1].y1;a.b=muc[1].y2;
b.a=muc[2].y1;b.b=muc[2].y2;
c.a=muc[3].y1;c.b=muc[3].y2;
intersecteaza(a,b,iab);
intersecteaza(a,c,iac);
intersecteaza(b,c,ibc);
intersecteaza(iab,c,iabc);
p=length(a)+length(b)-length(iab)+length(c)-length(iac)-length(ibc)+length(iabc);
p*=2;
}

double max(double a, double b) {if (a>b) return a; return b;}
double min(double a, double b) {if (a>b) return b; return a;}

void intersecteaza_d(DREPTUNGHI a, DREPTUNGHI b, DREPTUNGHI &c)
{
c.x1=max(a.x1,b.x1);
c.y1=max(a.y1,b.y1);
c.x2=min(a.x2,b.x2);
c.y2=min(a.y2,b.y2);
if (c.x1>c.x2||c.y1>c.y2) c.x1=c.x2=c.y1=c.y2=0;
}

void calculeaza_aria()
{
DREPTUNGHI a,b,c,iab,iac,ibc,iabc;
a=drept[1];
b=drept[2];
c=drept[3];
intersecteaza_d(a,b,iab);
intersecteaza_d(a,c,iac);
intersecteaza_d(b,c,ibc);
intersecteaza_d(iab,c,iabc);
double aux1=aria(a)-aria(iab);
double aux2=aria(b)-aria(iac);
double aux3=aria(c)-aria(ibc);
double aux4=aria(iabc);
aux1+=aux2;
aux3+=aux4;
s=aux1+aux3;
}

void scrie()
{
FILE *g=fopen("reuniune.out","w");
p1+=p2;
fprintf(g,"%.0f %.0f\n",s,p1);
fcloseall();
}

int main()
{
citire();
lenmuc=0;
pune_muchiile(lenmuc);
baleiaza(p1);
lenmuc=0;
pune_muchiile_2(lenmuc);
baleiaza(p2);
calculeaza_aria();
scrie();
return 0;
}