#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct{double x,y;}cplx;
typedef struct{double size;cplx center,orient;}square;
cplx newCplx(double x,double y){
cplx z;
z.x=x;
z.y=y;
return z;
}
square newSquare(double size,cplx center,cplx orient){
square sq;
sq.size=size;
sq.center=center;
sq.orient=orient;
return sq;
}
cplx add(cplx z1,cplx z2){return newCplx(z1.x+z2.x,z1.y+z2.y);}
cplx subt(cplx z1,cplx z2){return newCplx(z1.x-z2.x,z1.y-z2.y);}
cplx mult(cplx z1,cplx z2){
return newCplx(z1.x*z2.x-z1.y*z2.y,z1.x*z2.y+z1.y*z2.x);
}
cplx mult1(cplx z,double k){
return newCplx(z.x*k,z.y*k);
}
cplx w1,w2,z1,z2;
double xMin,xMax,yMin,yMax,dX,dY;
double k;
square fSq1(square sq){
double size=sq.size*0.8;
cplx orient=mult(sq.orient,w1);
cplx center=add(sq.center,mult(mult1(sq.orient,sq.size),z1));
return newSquare(size,center,orient);
}
square fSq2(square sq){
double size=sq.size*0.6;
cplx orient=mult(sq.orient,w2);
cplx center=add(sq.center,mult(mult1(sq.orient,sq.size),z2));
return newSquare(size,center,orient);
}
double fXMin(square sq,int l,int l0){
if(l==l0||sq.center.x-k*sq.size>xMin)return sq.center.x;
double x1=fXMin(fSq1(sq),l+1,l0);
double x2=fXMin(fSq2(sq),l+1,l0);
double x=fmin(sq.center.x,fmin(x1,x2));
if(x<xMin)xMin=x;
return x;
}
double fXMax(square sq,int l,int l0){
if(l==l0||sq.center.x+k*sq.size<xMax)return sq.center.x;
double x1=fXMax(fSq1(sq),l+1,l0);
double x2=fXMax(fSq2(sq),l+1,l0);
double x=fmax(sq.center.x,fmax(x1,x2));
if(x>xMax)xMax=x;
return x;
}
double fYMin(square sq,int l,int l0){
if(l==l0||sq.center.y-k*sq.size>yMin)return sq.center.y;
double y1=fYMin(fSq1(sq),l+1,l0);
double y2=fYMin(fSq2(sq),l+1,l0);
double y=fmin(sq.center.y,fmin(y1,y2));
if(y<yMin)yMin=y;
return y;
}
double fYMax(square sq,int l,int l0){
if(l==l0||sq.center.y+k*sq.size<yMax)return sq.center.y;
double y1=fYMax(fSq1(sq),l+1,l0);
double y2=fYMax(fSq2(sq),l+1,l0);
double y=fmax(sq.center.y,fmax(y1,y2));
if(y>yMax)yMax=y;
return y;
}
int main()
{
w1=newCplx(0.8,0.6);
w2=newCplx(0.6,-0.8);
z1=mult(subt(newCplx(-0.5,0.5),mult(newCplx(-0.4,-0.4),w1)),newCplx(0,-1));
z2=mult(subt(newCplx(0.5,0.5),mult(newCplx(0.3,-0.3),w2)),newCplx(0,-1));
xMin=xMax=yMin=yMax=0;
k=4.5*sqrt(2);
square sq=newSquare(1,newCplx(0,0),newCplx(0,1));
int l=0;
double dX=0,dY=0,dX0,dY0;
double eps=1e-14;
do{
l+=10;
dX0=dX,dY0=dY;
fXMin(sq,1,l),fXMax(sq,1,l),fYMin(sq,1,l),fYMax(sq,1,l);
dX=xMax-xMin,dY=yMax-yMin;
printf("L = %d\n",l);
printf("xMin = %18.15f\n",xMin);
printf("xMax = %18.15f\n",xMax);
printf("yMin = %18.15f\n",yMin);
printf("yMax = %18.15f\n",yMax);
printf(" dX = %18.15f\n",dX);
printf(" dY = %18.15f\n\n",dY);
}while(fabs(dX-dX0)>eps||fabs(dY-dY0)>eps);
return 0;
}