tic();

N=11; // N >= 3
M=2;

h=sqrt(3)*M;

T0=Listplot([0,2*h/3],[-M,-h/3], [M,-h/3], [0,2*h/3]);
T1=Listplot([0,-h/3],[-M/2,h/6], [M/2,h/6], [0,-h/3]);
L=M+4;

PP=list();
PP($+1)=[0,0];
PP($+1)=list([0,h/3], [-M/2,-h/6], [M/2,-h/6]);
for i=1:(N-2)
  subSet=Scaledata(PP(2), 1/(2^i), 1/(2^i));
  CenterPP=PP(i+1);
  L=list();
  for num=1:length(CenterPP)
    CenterPoint=CenterPP(num);
    tmp=Translatedata(subSet, CenterPoint(1), CenterPoint(2));
    L=lstcat(L, tmp);
  end
  PP($+1)=L;
end

TT=list();
TT($+1)=T0;
TT($+1)=T1;
for i=1:(N-2)
  subT=Scaledata(T1, 1/(2^i), 1/(2^i));
  CenterPP=PP(i+1);
  L=list();
  for num=1:length(CenterPP)
    CenterPoint=CenterPP(num);
    tmp=Translatedata(subT, CenterPoint(1), CenterPoint(2));
    L=lstcat(L, tmp);
  end
  TT($+1)=L;
end

Openfile('tex/Sierpinski_gasket_sample.tex');
Beginpicture('5cm');
Setcolor([0,1,1,0], 1);
Shade(T0, 0);
Shade(T1, 1);
r=2;
for i=3:N
  tmpL=TT(i);
  Setcolor([0,1,1,0], 1);
  for num=1:length(tmpL)
    Shade(tmpL(num), (N-i+2)^r/N^r*1);
  end
end
Setcolor('black', 1);
Endpicture(0);
Closefile();

Setwindow(T0);
Openfile('tex/Sierpinski_gasket_sample0.tex');
Beginpicture('2cm');
Shade(T0, 1);
Endpicture(0);
Closefile();

Openfile('tex/Sierpinski_gasket_sample1.tex');
Beginpicture('2cm');
Shade(T0, 1);
Shade(T1, 0);
Endpicture(0);
Closefile();

T=toc();
disp(T);
for ind=2:(N-1)
  Openfile(strcat(['tex/Sierpinski_gasket_sample', string(ind), '.tex']));
  Beginpicture('2cm');
  Shade(T0, 1);
  Shade(T1, 0);
  for i=3:(ind+1)
    tmpL=TT(i);
    for num=1:length(tmpL)
      Shade(tmpL(num), 0);
    end
  end
  Endpicture(0);
  Closefile();
  T=toc();
  disp(ind);
  disp(T);  
end

T=toc();
disp(T);
