
(********************************************************************)
(*                                                                  *)
(*  mandelbr.sd7  Display the Mandelbrot set                        *)
(*  Copyright (C) 2007  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 "float.s7i";
  include "complex.s7i";
  include "draw.s7i";
  include "keybd.s7i";

# Display the Mandelbrot set, that are points z[0] in the complex plane
# for which the sequence z[n+1] := z[n] ** 2 + z[0] (n >= 0) is bounded.
# Since this program is computing intensive it should be compiled with
# hi comp -O2 mandelbr


const integer: pix is 200;
const integer: max_iter is 256;
var float: startZoom is 1.3 / flt(pix);
var complex: startCenter is complex(-0.75, 0.0);

var float: zoom is startZoom;
var complex: center is startCenter;
var array color: colorTable is max_iter times black;
var text: screen is STD_NULL;
var PRIMITIVE_WINDOW: startWindow is PRIMITIVE_WINDOW.value;


const func integer: iterate (in complex: z0) is func
  result
    var integer: iter is 1;
  local
    var complex: z is complex.value;
  begin
    z := z0;
    while sqrAbs(z) < 4.0 and  # not diverged
        iter < max_iter do     # not converged
      z *:= z;
      z +:= z0;
      incr(iter);
    end while;
  end func;


const proc: displayMandelbrotSet (in complex: center, in float: zoom) is func
  local
    var integer: x is 0;
    var integer: y is 0;
    var complex: z0 is complex.value;
  begin
    for x range -pix to pix do
      for y range -pix to pix do
        z0 := center + complex(flt(x) * zoom, flt(y) * zoom);
        point(x + pix, y + pix, colorTable[iterate(z0)]);
      end for;
    end for;
  end func;


const proc: showHelp is func
  begin
    put(0, 0, startWindow, PSET);
    setPos(screen, 8, 48);
    writeln(screen, "HELP");
    setPos(screen, 9, 43);
    color(screen, light_cyan, black);
    writeln(screen, "Left mouse key:");
    setPos(screen, 10, 40);
    color(screen, white, black);
    writeln(screen, "Select a new center");
    setPos(screen, 11, 38);
    writeln(screen, "and magnify by factor 2.");
    setPos(screen, 12, 42);
    color(screen, light_cyan, black);
    writeln(screen, "Middle mouse key:");
    setPos(screen, 13, 40);
    color(screen, white, black);
    writeln(screen, "Select a new center.");
    setPos(screen, 14, 43);
    color(screen, light_cyan, black);
    writeln(screen, "Right mouse key:");
    setPos(screen, 15, 39);
    color(screen, white, black);
    writeln(screen, "Scale down by factor 2.");
    setPos(screen, 16, 47);
    color(screen, light_cyan, black);
    writeln(screen, "H:");
    setPos(screen, 16, 50);
    color(screen, white, black);
    writeln(screen, "Help");
    setPos(screen, 17, 47);
    color(screen, light_cyan, black);
    writeln(screen, "R:");
    setPos(screen, 17, 50);
    color(screen, white, black);
    writeln(screen, "Restart");
    setPos(screen, 18, 47);
    color(screen, light_cyan, black);
    writeln(screen, "Q:");
    setPos(screen, 18, 50);
    color(screen, white, black);
    writeln(screen, "Quit");
    setPos(screen, 11, 27);
    writeln(screen, "to");
    setPos(screen, 12, 23);
    writeln(screen, "leave help");
    setPos(screen, 13, 24);
    writeln(screen, "press any");
    setPos(screen, 14, 27);
    writeln(screen, "key");
  end func;


const proc: doCommand (inout char: ch) is func
  local
    var PRIMITIVE_WINDOW: savedWindow is PRIMITIVE_WINDOW.value;
  begin
    case upper(ch) of
      when {KEY_MOUSE1}:
        center := complex(center.re + zoom * flt(getxpos(KEYBOARD) - pix),
                          center.im + zoom * flt(getypos(KEYBOARD) - pix));
        zoom := zoom / 2.0;
      when {KEY_MOUSE2}:
        center := complex(center.re + zoom * flt(getxpos(KEYBOARD) - pix),
                          center.im + zoom * flt(getypos(KEYBOARD) - pix));
      when {KEY_MOUSE3}:
        zoom := zoom * 2.0;
      when {'R'}:
        zoom := startZoom;
        center := startCenter;
        put(0, 0, startWindow, PSET);
        ch := getc(KEYBOARD);
        doCommand(ch);
      when {'H'}:
        savedWindow := getPixmap(curr_win);
        showHelp;
        ch := getc(KEYBOARD);
        if ch not in {'q', 'Q', KEY_ESC} then
          put(0, 0, savedWindow, PSET);
          ch := getc(KEYBOARD);
          doCommand(ch);
        end if;
    end case;
  end func;


const proc: main is func
  local
    const integer: num_pix is 2 * pix + 1;
    var char: ch is ' ';
    var integer: col is 0;
  begin
    screen(num_pix, num_pix);
    clear(curr_win, black);
    screen := open(curr_win, 16);
    KEYBOARD := GRAPH_KEYBOARD;
    for col range 1 to pred(max_iter) do
      colorTable[col] := color(65535 - (col * 5003) mod 65535,
                                       (col * 257)  mod 65535,
                                       (col * 2609) mod 65535);
    end for;
    displayMandelbrotSet(center, zoom);
    startWindow := getPixmap(curr_win);
    setPos(screen, 8, 43);
    writeln(screen, "M A N D E L B R");
    setPos(screen, 9, 41);
    writeln(screen, "Copyright (C) 2007");
    setPos(screen, 10, 44);
    writeln(screen, "Thomas Mertes");
    setPos(screen, 11, 36);
    writeln(screen, "This program is free soft-");
    setPos(screen, 12, 36);
    writeln(screen, "ware under the terms of");
    setPos(screen, 13, 36);
    writeln(screen, "the GNU General Public");
    setPos(screen, 14, 36);
    writeln(screen, "License. Mandelbr is wri-");
    setPos(screen, 15, 36);
    writeln(screen, "tten in the Seed7 program-");
    setPos(screen, 16, 38);
    writeln(screen, "ming language. Homepage:");
    setPos(screen, 17, 40);
    writeln(screen, "seed7.sourceforge.net");
    setPos(screen, 12, 26);
    writeln(screen, "press");
    setPos(screen, 13, 28);
    writeln(screen, "H");
    setPos(screen, 14, 24);
    writeln(screen, "for help");
    DRAW_FLUSH;
    ch := getc(KEYBOARD);
    if upper(ch) = 'H' then
      showHelp;
      ch := getc(KEYBOARD);
    end if;
    if ch not in {'q', 'Q', KEY_ESC} then
      if ch not in {KEY_MOUSE1, KEY_MOUSE2, KEY_MOUSE3} then
        put(0, 0, startWindow, PSET);
        ch := getc(KEYBOARD);
      end if;
      doCommand(ch);
      while ch not in {'q', 'Q', KEY_ESC} do
        if ch in {'r', 'R', KEY_MOUSE1, KEY_MOUSE2, KEY_MOUSE3} then
          displayMandelbrotSet(center, zoom);
          DRAW_FLUSH;
        end if;
        ch := getc(KEYBOARD);
        doCommand(ch);
      end while;
    end if;
  end func;
