program flob133;

uses crt,crush13h;

var c : char;
    x1,add,a: integer;

Procedure Line(X1,Y1,X2,Y2,Col:Integer;Where:Word);
Var Deltax,S,Deltay,Dx1,Dy1,Dx2,Dy2,S1,S2:Real;
    I:Integer;
Begin
     Deltax:=X2-X1;
     Deltay:=Y2-Y1;
     Dx1:=Sgn(Deltax);
     Dy1:=Sgn(Deltay);
     Dx2:=Sgn(Deltax);
     Dy2:= 0;
     S1:=Abs(Deltax);
     S2:=Abs(Deltay);
     If Not (S1>S2) Then
     Begin
          Dx2:=0;
          Dy2:=Sgn(Deltay);
          S1:=Abs(Deltay);
          S2:=Abs(Deltax);
     End;
     S:=Int(S1/2);
     For I:=0 To Round(S1) Do
     Begin
          if x1<320 then if x1>-1 then if y1<200 then if y1>-1 then PutPixel(X1,Y1,Col,Where);
          S:=S+S2;
          If Not (S<S1) Then
          Begin
               S:=S-S1;
               X1:=X1+Round(Dx1);
               Y1:=Y1+Round(Dy1);
          End
          Else
          Begin
               X1:=X1+Round(dx2);
               Y1:=Y1+Round(Dy2);
          End;
     End;
End;

Procedure Line3(X1,Y1,X2,Y2,Col:Integer;Where:Word);
Var Deltax,S,Deltay,Dx1,Dy1,Dx2,Dy2,S1,S2:Real;
    I:Integer;
    col2: integer;
Begin
     Deltax:=X2-X1;
     Deltay:=Y2-Y1;
     Dx1:=Sgn(Deltax);
     Dy1:=Sgn(Deltay);
     Dx2:=Sgn(Deltax);
     Dy2:= 0;
     S1:=Abs(Deltax);
     S2:=Abs(Deltay);
     If Not (S1>S2) Then
     Begin
          Dx2:=0;
          Dy2:=Sgn(Deltay);
          S1:=Abs(Deltay);
          S2:=Abs(Deltax);
     End;
     S:=Int(S1/2);
     For I:=0 To Round(S1) Do
     Begin
          col2:=round((mem[where:x1+1+y1*320]+mem[where:x1-1+y1*320]+mem[where:x1+y1*320]+
           mem[where:x1+(y1+1)*320]+mem[where:x1+(y1-1)*320]+mem[where:x1-1+(y1-1)*320]+mem[where:x1-1+(y1+1)*320]+
           mem[where:x1+1+(y1-1)*320]+mem[where:x1+1+(y1+1)*320]+col)/10)+5;
          if x1<320 then if x1>-1 then if y1<200 then if y1>-1 then PutPixel(X1,Y1,col2,Where);

          S:=S+S2;
          If Not (S<S1) Then
          Begin
               S:=S-S1;
               X1:=X1+Round(Dx1);
               Y1:=Y1+Round(Dy1);
          End
          Else
          Begin
               X1:=X1+Round(dx2);
               Y1:=Y1+Round(Dy2);
          End;
     End;
End;

begin
     video_mode ( $13);
     initvirt;

     x1:=0;
     add:=1;
     for a:=0 to 256 do setcolor (a,a div 4,a div 4,a div 4);

     repeat
      x1:=x1+add;
      if x1>318 then add:=-1;
      if x1<1 then add:=1;

      cls(0,vp[2]);

      line (0,x1,319,0,120,vp[2]);
      line (0,0,319,x1,120,vp[2]);
      move(mem[vp[2]:0],mem[vga:0],64000);
      waitvbl;

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

     closevirt;
     video_mode ( 03);
end.

