
program LIJN (input, output);

var
  X,Y, X2, Y2 : integer;
  TEKST       : string;
  CODE        : integer;
  FCOLOR      : integer;

  procedure WISSEL (var A : integer; var B : integer);
    var
      HELP : integer;
    begin
      HELP:= A;
      A:= B;
      B:= HELP;
    end;

procedure POINTMCGA (X, Y : integer);
  begin
    mem[$A000:X + Y*320]:= FColor;
  end;

procedure LINEMCGA (X, Y : integer; X2, Y2 : integer);
  var
    dx, dy   : longint;
    m        : real;
    xvar,yvar: real;
    C        : integer;
  begin
    if X2 < X then WISSEL (X, X2);
    if Y2 < Y then WISSEL (Y, Y2);
    if X=X2 then begin
      for C:= Y to Y2 do
        PointMCGA (X, C);
    end
      else
        if Y=Y2 then begin
          for C:= X to X2 do
            PointMCGA (C, Y);
        end else begin
          dx:= abs (x2-x);
          dy:= abs (y2-y);
          m:= dy/dx;
          xvar:= x;
          repeat
            yvar:= y+ m*(xvar-x);
            PointMCGA (round (xvar), round (yvar));
            xvar:= xvar + 0.33;
          until xvar > x2;
        end;
  end;

procedure CHECKCODE;
  var
    OK   : boolean;
  begin
    OK:= (CODE = 0);
    if not OK then begin
      writeln ('Fout in integer constante in ', paramstr(0), ' -programma');
      halt (31);
    end;
  end;

begin
  if paramcount < 5 then begin
    writeln;
    writeln (' dit programma tekent een lijn op het mcga scherm');
    writeln;
    writeln ('    gebruik: LIJN[.EXE] <X1> <Y1> <X2> <Y2> <KLEUR>....');
    halt (30);
  end;
  TEKST:= paramstr (1);
  val (TEKST, X, CODE);
  CheckCode;
  TEKST:= paramstr (2);
  val (TEKST, Y, CODE);
  CheckCode;
  TEKST:= paramstr (3);
  val (TEKST, X2, CODE);
  CheckCode;
  TEKST:= paramstr (4);
  val (TEKST, Y2, CODE);
  CheckCode;
  TEKST:= paramstr (5);
  val (TEKST, FCOLOR, CODE);
  CheckCode;
  if (X2 < X) then WISSEL (X, X2);
  if (Y2 < Y) then WISSEL (Y, Y2);
  LINEMCGA (X,Y, X2, Y2);
end.
