
(********************************************************************)
(*                                                                  *)
(*  hilbert.sd7   Display a Hilbert curve.                          *)
(*  Copyright (C) 2011  Thomas Mertes                               *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "draw.s7i";
  include "keybd.s7i";

const integer: delta is 8;

const proc: drawDown (inout integer: x, inout integer: y, in integer: n) is forward;
const proc: drawUp (inout integer: x, inout integer: y, in integer: n) is forward;

const proc: drawRight (inout integer: x, inout integer: y, in integer: n) is func
  begin
    if n > 0 then
      drawDown(x, y, pred(n));
      line(x, y, 0, delta, white);
      y +:= delta;
      drawRight(x, y, pred(n));
      line(x, y, delta, 0, white);
      x +:= delta;
      drawRight(x, y, pred(n));
      line(x, y, 0, -delta, white);
      y -:= delta;
      drawUp(x, y, pred(n));
    end if;
  end func;

const proc: drawLeft (inout integer: x, inout integer: y, in integer: n) is func
  begin
    if n > 0 then
      drawUp(x, y, pred(n));
      line(x, y, 0, -delta, white);
      y -:= delta;
      drawLeft(x, y, pred(n));
      line(x, y, -delta, 0, white);
      x -:= delta;
      drawLeft(x, y, pred(n));
      line(x, y, 0, delta, white);
      y +:= delta;
      drawDown(x, y, pred(n));
    end if;
  end func;

const proc: drawDown (inout integer: x, inout integer: y, in integer: n) is func
  begin
    if n > 0 then
      drawRight(x, y, pred(n));
      line(x, y, delta, 0, white);
      x +:= delta;
      drawDown(x, y, pred(n));
      line(x, y, 0, delta, white);
      y +:= delta;
      drawDown(x, y, pred(n));
      line(x, y, -delta, 0, white);
      x -:= delta;
      drawLeft(x, y, pred(n));
    end if;
  end func;

const proc: drawUp (inout integer: x, inout integer: y, in integer: n) is func
  begin
    if n > 0 then
      drawLeft(x, y, pred(n));
      line(x, y, -delta, 0, white);
      x -:= delta;
      drawUp(x, y, pred(n));
      line(x, y, 0, -delta, white);
      y -:= delta;
      drawUp(x, y, pred(n));
      line(x, y, delta, 0, white);
      x +:= delta;
      drawRight(x, y, pred(n));
    end if;
  end func;

const proc: main is func
  local
    var integer: x is 11;
    var integer: y is 11;
  begin
    screen(526, 526);
    KEYBOARD := GRAPH_KEYBOARD;
    drawRight(x, y, 6);
    readln(KEYBOARD);
  end func;
