Fixed >scaled in example.
[forth.jl.git] / examples / mandelbrot.4th
1 ( --- Complex arithmetic --- )
2
3 ( Location of floating point. )
4 : precision 10000 ;
5
6 : sign dup abs / ;
7
8 : >scaled
9     precision 10 / * over
10     ?dup 0<> if 
11         sign *
12     then
13     swap precision * +
14 ;
15
16 ( Redefine multiplication.  Yay forth! )
17 : * precision */ ;
18
19 : c* ( x1 y1 x2 y2 -- x3 y3 )
20         swap -rot               ( x1 x2 y1 y2 )
21         2dup * negate           ( x1 x2 y1 y2 -y1y2 )
22         4 pick 4 pick * +       ( x1 x2 y1 y2 (x1x2-y1y2))
23         4 roll 2 roll *         ( x2 y1 (x1x2-y1y2) x1y2 )
24         3 roll 3 roll * +       ( (x1x2-y1y2) (x1y2+x2y1) )
25 ;
26
27 : c+ ( x1 y1 x2 y2 -- x3 y3 )
28         rot +
29         -rot +
30         swap
31 ;
32
33 : csq 2dup c* ;
34
35 : cmagsq ( x1 y1 -- mag )
36         csq abs
37 ;
38
39 ( --- Mandelbrot set calculations  --- )
40
41 : iterate ( cr ci zr zi -- cr ci z'r z'i )
42         csq c+
43 ;
44
45 : inSet? ( cr ci -- res )
46     
47     100 0 do
48
49         2swap 2dup 5 roll 5 roll
50         iterate
51         2dup cmagsq
52         100 > if
53             leave
54         then
55
56     loop
57 ;
58
59 : xsteps 100 ;
60 : ysteps 50 ;
61
62 ( Draw the Mandelbrot Set!)
63 : mandel ( x1 y1 x2 y2 -- )
64
65     1 pick 4 pick -
66     2 pick 5 pick do
67         i 0 inSet? if
68             42 emit
69         else
70             '.' emit
71         then
72     dup +loop
73 ;
74
75 ( Clean up - hide non-standard multiplication def. )
76 hide *