// Last edited on DATE TIME by USER
// Processed by remove-cam-lights

#include "colors.inc"
#include "eixos.inc"
#include "retalho.inc"

background{ color rgb < 0.75, 0.80, 0.85 > }

#declare tx_plastico =
  texture{
    pigment{ color rgb < 0.90, 0.80, 1.00 > }
    finish{ diffuse 0.8 ambient 0.1 specular 0.5 roughness 0.005 }
  }

#declare tx_fosca =
  texture{
    pigment{ color rgb < 1.00, 0.80, 0.10 > }
    finish{ diffuse 0.9 ambient 0.1 }
  }

#macro bolinha(cor)
 sphere {
    <0,0,0>, 0.1
    pigment { color cor }
  }
#end

#macro interpola(t0, v0, t1, v1, tt)
  #local ss = (tt-t0)/(t1-t0);
  #local rr = 1-ss;

  (rr*v0 + ss*v1)

#end

#macro interpola4(t0,v0,v1,v2,v3,t1,tt)

  #local v01 = interpola(t0, v0, t1, v1, tt);
  #local v12 = interpola(t0, v1, t1, v2, tt);
  #local v23 = interpola(t0, v2, t1, v3, tt);

  #local v012 = interpola(t0, v01, t1, v12, tt);
  #local v123 = interpola(t0, v12, t1, v23, tt);

  interpola(t0, v012, t1, v123, tt);

#end

#macro grafico(x0,y0,y1,y2,y3,x3,N,cor)
  #local i = (x3-x0)/(N-1);
  #local xx = x0;

  union{
    #while(xx<=x3)
      #local yy = interpola4(x0,y0,y1,y2,y3,x3,xx);
      object{ bolinha(cor) translate <xx,yy,0> }
      #local xx = xx + i;
    #end
  }

#end

#macro grafico3d(t0,y0,y1,y2,y3,t3,cor)
  #local i = (t3-t0)/(N-1);
  #local tt = t0;

  union{
    #while(tt<=t3)
      #local yy = interpola4(t0,y0,y1,y2,y3,t3,tt);
      object{ bolinha(cor) translate yy }
      #local tt = tt + i;
    #end
  }

#end

#macro grafico4(y10,y11,y12,y21,y22,y31,y32,y41,y42,y43,N)
  #local p1 = (y12+y21)/2;
  #local p2 = (y22+y31)/2;
  #local p3 = (y32+y41)/2;

  union{
    object{ grafico(0,y10,y11,y12,p1,5,N,Blue) }
    object{ grafico(5,p1,y21,y22,p2,10,N,Red) }
    object{ grafico(10,p2,y31,y32,p3,15,N,Black) }
    object{ grafico(15,p3,y41,y42,y43,20,N,Green) }
  }

#end

#macro grafico4_3d(tt,y10,y11,y12,y21,y22,y31,y32,y41,y42,y43,N)
  #local p1 = (y12+y21)/2;
  #local p2 = (y22+y31)/2;
  #local p3 = (y32+y41)/2;

  union{
    object{ grafico3d(0,y10,y11,y12,p1,tt/4,N,Blue) }
    object{ grafico3d(tt/4,p1,y21,y22,p2,tt/2,N,Red) }
    object{ grafico3d(tt/2,p2,y31,y32,p3,3*tt/4,N,Black) }
    object{ grafico3d(3*tt/4,p3,y41,y42,y43,tt,N,Green) }
  }

#end

#macro grafico4_3d_fechado(tt,y11,y12,y21,y22,y31,y32,y41,y42,N)
  #local p1 = (y11+y42)/2;
  #local p2 = (y12+y21)/2;
  #local p3 = (y22+y31)/2;
  #local p4 = (y32+y41)/2;

  union{
    object{ grafico3d(0,p1,y11,y12,p2,tt/4,N,Blue) }
    object{ grafico3d(tt/4,p2,y21,y22,p3,tt/2,N,Red) }
    object{ grafico3d(tt/2,p3,y31,y32,p4,3*tt/4,N,White) }
    object{ grafico3d(3*tt/4,p4,y41,y42,p1,tt,N,Green) }
  }
#end

#macro grafico4_3d_fechado_fita(tt,y11,y12,y21,y22,y31,y32,y41,y42)
  #local p1 = (y11+y42)/2;
  #local p2 = (y12+y21)/2;
  #local p3 = (y22+y31)/2;
  #local p4 = (y32+y41)/2;

  #if(tt<0.25)
    object{ grafico_fita(0,p1,y11,y12,p2,0.25,tt) }
  #else
    #if(tt<0.5)
      object{ grafico_fita(0.25,p2,y21,y22,p3,0.5,tt) }
    #else
      #if(tt<0.75)
        object{ grafico_fita(0.5,p3,y31,y32,p4,0.75,tt) }
      #else
        object{ grafico_fita(0.75,p4,y41,y42,p1,1,tt) }
      #end
    #end
  #end

#end

#declare r11=<2,-1,2>;
#declare r12=<2,-1,4>;
#declare r13=<2,-1,6>;
#declare r14=<2,-1,8>;

#declare r21=<3,0,2>;
#declare r22=<3,0,4>;
#declare r23=<3,0,6>;
#declare r24=<3,0,8>;

#declare r31=<1,2,2>;
#declare r32=<1,2,4>;
#declare r33=<1,2,6>;
#declare r34=<1,2,8>;

#declare r41=<0,3,2>;
#declare r42=<0,3,4>;
#declare r43=<0,3,6>;
#declare r44=<0,3,8>;

#declare q11=<5,1,1>;
#declare q12=<5,1,2>;
#declare q13=<5,1,3>;
#declare q14=<5,1,4>;

#declare q21=<3,2,1>;
#declare q22=<3,2,2>;
#declare q23=<3,2,3>;
#declare q24=<3,2,4>;

#declare q31=<2,5,1>;
#declare q32=<2,5,2>;
#declare q33=<2,5,3>;
#declare q34=<2,5,4>;

#declare q41=<1,3,1>;
#declare q42=<1,3,2>;
#declare q43=<1,3,3>;
#declare q44=<1,3,4>;

#declare s11=<0,1,2>;
#declare s12=<0,1,4>;
#declare s13=<0,1,6>;
#declare s14=<0,1,8>;

#declare s21=<-2,0,1>;
#declare s22=<-2,0,2>;
#declare s23=<-2,0,3>;
#declare s24=<-2,0,4>;

#declare s31=<2,2,1>;
#declare s32=<2,2,3>;
#declare s33=<2,2,5>;
#declare s34=<2,2,7>;

#declare s41=<1,4,1>;
#declare s42=<1,4,2>;
#declare s43=<1,4,3>;
#declare s44=<1,4,4>;

#macro retalho4()
  #local p1 = (q11 +r41)/2;
  #local p2 = (q12 +r42)/2;
  #local p3 = (q13 +r43)/2;
  #local p4 = (q14 +r44)/2;
  #local t1 = (q41 +s11)/2;
  #local t2 = (q42 +s12)/2;
  #local t3 = (q43 +s13)/2;
  #local t4 = (q44 +s14)/2;

  union{
    object{retalho(r11,r12,r13,r14,r21,r22,r23,r24,r31,r32,r33,r34,p1,p2,p3,p4,0,texture{pigment {color White}}, texture{pigment {color Blue}})}
    object{retalho(p1,p2,p3,p4,q21,q22,q23,q24,q31,q32,q33,q34,t1,t2,t3,t4,0,texture{pigment {color White}}, tx_plastico) }
    object{retalho(t1,t2,t3,t4,r21,r22,r23,r24,r31,r32,r33,r34,p1,p2,p3,p4,0,texture{pigment {color White}}, texture{pigment {color Red}})}
  }

#end

#macro grafico_fita(t0,y0,y1,y2,y3,t3,tt)
  #local yy = interpola4(t0,y0,y1,y2,y3,t3,tt);
  object{ bolinha(Red) translate yy }
#end

#macro retalho4_fita(t0,y0,y1,y2,y3,t3,tt)
  #local dt = 1/10;
  #local pos = array[11];
  #local tempo = tt;
  #local pos[0] = interpola4(t0,y0,y1,y2,y3,t3,tempo);

  #local i = 1;
  #while (i<=10)
    #local tempo = tempo-dt;
    #if(tempo < 0 )
      #local tempo = tempo+1;
    #end

    #if(tempo<0.25)
      #local pos[i] = interpola4(0,y0,y1,y2,y3,0.25,tempo);
    #else
      #if(tempo<0.5)
        #local pos[i] = interpola4(0.25,y0,y1,y2,y3,0.5,tempo);
      #else
        #if(tempo<0.75)
          #local pos[i] = interpola4(0.5,y0,y1,y2,y3,0.75,tempo);
        #else
          #local pos[i] = interpola4(0.75,y0,y1,y2,y3,1,tempo);
        #end
      #end
    #end

    #local i = i+1;
  #end

  #local p1 = (pos[3]+pos[5])/2;
  #local t1 = (pos[6]+pos[8])/2;

  #local h1 = <0,0,0>;
  #local h2 = <0,0,0>;
  #local h3 = <0,0,0>;
  #local hh1 = <0,0,0>;
  #local hh2 = <0,0,0>;
  #local hh3 = <0,0,0>;

  union{
    object{retalho(pos[0],pos[0]+h1,pos[0]+h2,pos[0]+h3,pos[1],pos[1]+hh1,pos[1]+hh2,pos[1]+hh3,pos[3],pos[3]+h1,pos[3]+h2,pos[3]+h3,p1,p1+hh1,p1+hh2,p1+hh3,0.01,texture{pigment {color White}}, texture{pigment {color Blue}})}
    object{retalho(p1,p1+hh1,p1+hh2,p1+hh3,pos[5],pos[5]+h1,pos[5]+h2,pos[5]+h3,pos[6],pos[6]+hh1,pos[6]+hh2,pos[6]+hh3,t1,t1+h1,t1+h2,t1+h3,0.01,texture{pigment {color White}}, tx_plastico) }
    object{retalho(t1,t1+h1,t1+h2,t1+h3,pos[8],pos[8]+hh1,pos[8]+hh2,pos[8]+hh3,pos[9],pos[9]+h1,pos[9]+h2,pos[9]+h3,pos[10],pos[10]+hh1,pos[10]+hh2,pos[10]+hh3,0.01,texture{pigment {color White}}, texture{pigment {color Red}})}
  }

#end

union{

  object { grafico4_3d_fechado_fita(clock,<1,5,3>,<2,3,2>,<5,8,2>,<7,-2,2>,<2,3,1>,<1,-3,2>,<1,3,0>,<-5,0,2>)  }

}

#include "camlight.inc"
#declare centro_cena = < 0.00, 0.00, 0.00 >;
#declare raio_cena = 10.0;
#declare dir_camera = <0,-0.01,1>; //< 14.00, 7.00, 4.00 >;
#declare dist_camera = 50.0;
#declare intens_luz = 1.00;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)