PROGRAM hilbert;

{$I GRAPHICS.INC}

CONST h0 = 256;
      n  = 5;

VAR   i, x, y, h, x0, y0 : integer;

{$A-}
PROCEDURE b (i : integer); FORWARD;
PROCEDURE c (i : integer); FORWARD;
PROCEDURE d (i : integer); FORWARD;

PROCEDURE a (i : integer);

BEGIN
  IF i > 0
    THEN
      BEGIN
        d (i-1);
        x := x-3*h;
        moveto (x, y);
        a (i-1);
        y := y-h;
        moveto (x, y);
        a (i-1);
        x := x+3*h;
        moveto (x, y);
        b (i-1)
      END
END;


PROCEDURE b;

BEGIN
  IF i > 0
    THEN
      BEGIN
        c (i-1);
        y := y+h;
        moveto (x, y);
        b (i-1);
        x := x+3*h;
        moveto (x, y);
        b (i-1);
        y := y-h;
        moveto (x, y);
        a (i-1)
      END
END;

PROCEDURE c;

BEGIN
  IF i > 0
    THEN
      BEGIN
        b (i-1);
        x := x+3*h;
        moveto (x, y);
        c (i-1);
        y := y+h;
        moveto (x, y);
        c (i-1);
        x := x-3*h;
        moveto (x, y);
        d (i-1)
      END
END;

PROCEDURE d;

BEGIN
  IF i > 0
    THEN
      BEGIN
        a (i-1);
        y := y-h;
        moveto (x, y);
        d (i-1);
        x := x-3*h;
        moveto (x, y);
        d (i-1);
        y := y+h;
        moveto (x, y);
        c (i-1)
      END
END;

{$A+}

BEGIN { hilbert }
  clrgbs;
  clrtbs;
  i := 0;
  h := h0;
  x0 := 2*h+50;
  y0 := h DIV 2;
  quad (0, 0, xmax, ymax);
  REPEAT
    i := i+1;
    h := h DIV 2;
    x0 := x0+(h DIV 2);
    y0 := y0+(h DIV 2);
    x := x0;
    y := y0;
    penup;
    moveto (x, y);
    pendown;
    a (i)
  UNTIL i = n;
  hardcopy2
END.  { hilbert }