186 lines
5.9 KiB
INI
186 lines
5.9 KiB
INI
"Mandelbrot"
|
|
|
|
The story headline is "A Non-Interactive Set".
|
|
|
|
Include Glimmr Drawing Commands by Erik Temple.
|
|
|
|
[Q20 fixed-point or floating-point: see definitions below]
|
|
Use floating-point math.
|
|
|
|
Finished is a room.
|
|
|
|
The graphics-window is a graphics g-window spawned by the main-window.
|
|
The position is g-placeabove.
|
|
|
|
When play begins:
|
|
let f10 be 10 as float;
|
|
now min re is ( -20 as float ) fdiv f10;
|
|
now max re is ( 6 as float ) fdiv f10;
|
|
now min im is ( -12 as float ) fdiv f10;
|
|
now max im is ( 12 as float ) fdiv f10;
|
|
now max iterations is 100;
|
|
add color g-Black to the palette;
|
|
add color g-Red to the palette;
|
|
add hex "#FFA500" to the palette;
|
|
add color g-Yellow to the palette;
|
|
add color g-Green to the palette;
|
|
add color g-Blue to the palette;
|
|
add hex "#4B0082" to the palette;
|
|
add hex "#EE82EE" to the palette;
|
|
open up the graphics-window.
|
|
|
|
Min Re is a number that varies.
|
|
Max Re is a number that varies.
|
|
Min Im is a number that varies.
|
|
Max Im is a number that varies.
|
|
|
|
Max Iterations is a number that varies.
|
|
|
|
Min X is a number that varies.
|
|
Max X is a number that varies.
|
|
Min Y is a number that varies.
|
|
Max Y is a number that varies.
|
|
|
|
The palette is a list of numbers that varies.
|
|
|
|
[vertically mirrored version]
|
|
Window-drawing rule for the graphics-window when max im is fneg min im:
|
|
clear the graphics-window;
|
|
let point be { 0, 0 };
|
|
now min X is 0 as float;
|
|
now min Y is 0 as float;
|
|
let mX be the width of the graphics-window minus 1;
|
|
let mY be the height of the graphics-window minus 1;
|
|
now max X is mX as float;
|
|
now max Y is mY as float;
|
|
let L be the column order with max mX;
|
|
repeat with X running through L:
|
|
now entry 1 in point is X;
|
|
repeat with Y running from 0 to mY / 2:
|
|
now entry 2 in point is Y;
|
|
let the scaled point be the complex number corresponding to the point;
|
|
let V be the Mandelbrot result for the scaled point;
|
|
let C be the color corresponding to V;
|
|
if C is 0, next;
|
|
draw a rectangle (C) in the graphics-window at the point with size 1 by 1;
|
|
now entry 2 in point is mY - Y;
|
|
draw a rectangle (C) in the graphics-window at the point with size 1 by 1;
|
|
yield to VM;
|
|
rule succeeds.
|
|
|
|
[slower non-mirrored version]
|
|
Window-drawing rule for the graphics-window:
|
|
clear the graphics-window;
|
|
let point be { 0, 0 };
|
|
now min X is 0 as float;
|
|
now min Y is 0 as float;
|
|
let mX be the width of the graphics-window minus 1;
|
|
let mY be the height of the graphics-window minus 1;
|
|
now max X is mX as float;
|
|
now max Y is mY as float;
|
|
let L be the column order with max mX;
|
|
repeat with X running through L:
|
|
now entry 1 in point is X;
|
|
repeat with Y running from 0 to mY:
|
|
now entry 2 in point is Y;
|
|
let the scaled point be the complex number corresponding to the point;
|
|
let V be the Mandelbrot result for the scaled point;
|
|
let C be the color corresponding to V;
|
|
if C is 0, next;
|
|
draw a rectangle (C) in the graphics-window at the point with size 1 by 1;
|
|
yield to VM;
|
|
rule succeeds.
|
|
|
|
To decide which list of numbers is column order with max (N - number):
|
|
let L be a list of numbers;
|
|
let L2 be a list of numbers;
|
|
let D be 64;
|
|
let rev be false;
|
|
while D > 0:
|
|
let X be 0;
|
|
truncate L2 to 0 entries;
|
|
while X <= N:
|
|
if D is 64 or X / D is odd, add X to L2;
|
|
increase X by D;
|
|
if rev is true:
|
|
reverse L2;
|
|
let rev be false;
|
|
otherwise:
|
|
let rev be true;
|
|
add L2 to L;
|
|
let D be D / 2;
|
|
decide on L.
|
|
|
|
To decide which list of numbers is complex number corresponding to (P - list of numbers):
|
|
let R be a list of numbers;
|
|
extend R to 2 entries;
|
|
let X be entry 1 in P as float;
|
|
let X be (max re fsub min re) fmul (X fdiv max X);
|
|
let X be X fadd min re;
|
|
let Y be entry 2 in P as float;
|
|
let Y be (max im fsub min im) fmul (Y fdiv max Y);
|
|
let Y be Y fadd min im;
|
|
now entry 1 in R is X;
|
|
now entry 2 in R is Y;
|
|
decide on R.
|
|
|
|
To decide which number is Mandelbrot result for (P - list of numbers):
|
|
let c_re be entry 1 in P;
|
|
let c_im be entry 2 in P;
|
|
let z_re be 0 as float;
|
|
let z_im be z_re;
|
|
let threshold be 4 as float;
|
|
let runs be 0;
|
|
while 1 is 1:
|
|
[ z = z * z ]
|
|
let r2 be z_re fmul z_re;
|
|
let i2 be z_im fmul z_im;
|
|
let ri be z_re fmul z_im;
|
|
let z_re be r2 fsub i2;
|
|
let z_im be ri fadd ri;
|
|
[ z = z + c ]
|
|
let z_re be z_re fadd c_re;
|
|
let z_im be z_im fadd c_im;
|
|
let norm be (z_re fmul z_re) fadd (z_im fmul z_im);
|
|
increase runs by 1;
|
|
if norm is greater than threshold, decide on runs;
|
|
if runs is max iterations, decide on 0.
|
|
|
|
To decide which number is color corresponding to (V - number):
|
|
let L be the number of entries in the palette;
|
|
let N be the remainder after dividing V by L;
|
|
decide on entry (N + 1) in the palette.
|
|
|
|
Section - Fractional numbers (for Glulx only)
|
|
|
|
To decide which number is (N - number) as float: (- (numtof({N})) -).
|
|
To decide which number is (N - number) fadd (M - number): (- (fadd({N}, {M})) -).
|
|
To decide which number is (N - number) fsub (M - number): (- (fsub({N}, {M})) -).
|
|
To decide which number is (N - number) fmul (M - number): (- (fmul({N}, {M})) -).
|
|
To decide which number is (N - number) fdiv (M - number): (- (fdiv({N}, {M})) -).
|
|
To decide which number is fneg (N - number): (- (fneg({N})) -).
|
|
To yield to VM: (- glk_select_poll(gg_event); -).
|
|
|
|
Use Q20 fixed-point math translates as (- Constant Q20_MATH; -).
|
|
Use floating-point math translates as (- Constant FLOAT_MATH; -).
|
|
|
|
Include (-
|
|
#ifdef Q20_MATH;
|
|
! Q11.20 format: 1 sign bit, 11 integer bits, 20 fraction bits
|
|
[ numtof n r; @shiftl n 20 r; return r; ];
|
|
[ fadd n m; return n+m; ];
|
|
[ fsub n m; return n-m; ];
|
|
[ fmul n m; n = n + $$1000000000; @sshiftr n 10 n; m = m + $$1000000000; @sshiftr m 10 m; return n * m; ];
|
|
[ fdiv n m; @sshiftr m 20 m; return n / m; ];
|
|
[ fneg n; return -n; ];
|
|
#endif;
|
|
|
|
#ifdef FLOAT_MATH;
|
|
[ numtof f; @"S2:400" f f; return f; ];
|
|
[ fadd n m; @"S3:416" n m n; return n; ];
|
|
[ fsub n m; @"S3:417" n m n; return n; ];
|
|
[ fmul n m; @"S3:418" n m n; return n; ];
|
|
[ fdiv n m; @"S3:419" n m n; return n; ];
|
|
[ fneg n; @bitxor n $80000000 n; return n; ];
|
|
#endif;
|
|
-).
|