program tritunnel;

uses crush13h,crt;

type trirec=record
                  x1,y1,x2,y2,x3,y3:real;
                  col:integer;
            end;

var  tri:array[0..255] of trirec;
     c:char;
     a,aa:integer;
     count1,add1:integer;
     ang1,ang2:real;

Procedure Rotate(Var X, Y, ang : Real);
        Var XX, YY : Real;
        Begin
           XX := X*Cos(ang)+Y*Sin(ang);
      YY := Y*Cos(ang)-X*Sin(ang);
      X := XX;
      Y := YY;
End;

procedure blur(from,too:word);
var x,y,aaa,ccc,counter:integer;
    cccc: array [1..9] of integer;
begin
     for x:=1 to 318 do for y:=1 to 198 do
         begin
          cccc[1]:=mem[from:(x+1)+(y+1)*320];
          cccc[2]:=mem[from:(x)+(y+1)*320];
          cccc[3]:=mem[from:(x-1)+(y+1)*320];
          cccc[4]:=mem[from:(x+1)+(y)*320];
          cccc[5]:=mem[from:(x)+(y)*320];
          cccc[6]:=mem[from:(x-1)+(y)*320];
          cccc[7]:=mem[from:(x+1)+(y-1)*320];
          cccc[8]:=mem[from:(x)+(y-1)*320];
          cccc[9]:=mem[from:(x-1)+(y-1)*320];
          ccc:=0;
          counter:=0;
          for aaa:=1 to 9 do
              begin
                   {if cccc[aaa]<>0 then
                      begin}
                           counter:=counter+1;
                           ccc:=ccc+cccc[aaa];
                    {  end;  }
              end;
          mem[too:x+y*320]:=round(ccc div counter);
      end;
end;

procedure blur_f(from,too:word);
var x,y,aaa,ccc,ccc2,counter:integer;
    cccc: array [1..9] of integer;
begin
     for x:=1 to 318 do for y:=1 to 198 do
         begin
          cccc[1]:=mem[from:(x)+(y+1)*320];
          cccc[2]:=mem[from:(x+1)+(y)*320];
          cccc[3]:=mem[from:(x)+(y)*320];
          cccc[4]:=mem[from:(x-1)+(y)*320];
          cccc[5]:=mem[from:(x)+(y-1)*320];

               ccc2:=(((cccc[1]+cccc[2]+cccc[4]+cccc[5]) div 4) + cccc[3]) div 2;
               mem[too:x+y*320]:=ccc2;

         end;
end;

begin
     initvirt;
     video_mode ( $13);

     randomize;

     ang1:=(3);
     count1:=50;
     add1:=1;


     for a:=0 to 255 do setcolor (a,a div 4,a div 4, a div 4);

     repeat
      {cls(0,vp[2]);}

      count1:=count1+add1;
      if count1>100 then add1:=-1;
      if count1<50 then add1:=1;

      {for a:=0 to 255 do
      with tri[a] do
           begin
              rotate (x1,y1,ang1);
              rotate (x2,y2,ang1);
              rotate (x3,y3,ang1);
           end;}

      for a:=0 to 255 do
      with tri[a] do
           begin
                ang2:=random(90);
                tri[a].x1:=cos(ang2);
                tri[a].y1:=sin(ang2);
                tri[a].x2:=cos(ang2+Pi/(count1/100));
                tri[a].y2:=sin(ang2+Pi/(count1/100));
                line(160+round(x1*a),100-round(y1*a),160+round(x2*a),100-round(y2*a),a,vp[2]);
           end;
           waitvbl;
      blur_f (vp[2],vp[1]);
      move (mem[vp[1]:0],mem[vga:0],64000);

      if keypressed=true then c:=readkey;
     until c=#27;

     video_mode ( 03);
     closevirt;
end.