\ From: "Ed" \ Message-ID: \ This version translated to Forth. \ Originally programmed in XPL0 by ... \ \ MANDEL.XPL APR-27-99 \ Mandelbrot Set Plotter \ Loren Blaney \ \ The Mandelbrot set is the set of points C that do not cause Z to go to \ infinity when this equation is repeatedly calculated: \ \ Z := Z^2 + C \ \ Z and C are complex numbers (Z^2 is Z squared). They consist of two values \ called real and imaginary, which are indicated like this: \ \ Z = Zr + Zi*i \ C = Cr + Ci*i \ \ Z^2 = (Zr + Zi*i)^2 = Zr^2 + 2*Zr*Zi*i + Zi^2*i^2 \ \ By definition i^2 = -1, thus: \ \ Z^2 = Zr^2 - Zi^2 + 2*Zr*Zi*i \ \ Keeping the real and imaginary parts separate, the original equation becomes: \ \ Zr':= Zr^2 - Zi^2 + Cr \ Zi := 2*Zr*Zi + Ci \ Zr := Zr' \ (Zr' is simply to keep from changing Zr prematurely.) \ \ Plotting the points C in the Mandelbrot set gives the characteristic snowman \ shape. The real part (Cr) is plotted on the X axis, and the imaginary part \ (Ci) is plotted on the Y axis. If colors are plotted according to the rate \ that Z approaches infinity, beautiful patterns emerge. ( empty) forth definitions decimal ( application) \ 1 fload XPLGRAPH \ Define functions we don't have: : FLOAT ( n -- ; R: -- r ) s>d d>f ; : F> ( R: r1 r2 -- ; -- flag ) fswap f< ; : f<= ( r1 r2 -- f ) fswap f< 0= ; \ variable X \ Screen coordinates of current point \ variable Y ( don't need X,Y - will be using forth's built-in loop indexes I,J ) variable Cnt \ Iteration counter fvariable Cr \ Coordinates scaled to +/-2 range fvariable Ci \ fvariable Temp \ Temporary scratch ( don't need this either ) : ShowMandel ( -- ) 300 0 do \ For the top half of the screen... 800 0 do \ For all the points horizontally... i float 800.0e f/ 0.5e f- 4.0e f* Cr f! \ Range -2.0 to +2.0 j float 300.0e f/ 1.5e f* Ci f! \ Range 0.0 to +1.5 0 0e 0e begin ( count zr zi ) fover fdup f* fover fdup f* fover fover f+ 2e f<= while ( count zr zi zr^2 zi^2 ) f- Cr f@ f+ ( count zr zi zr' ) frot frot f* fdup f+ Ci f@ f+ ( count zr' zi' ) 1+ dup 64 > until else fdrop fdrop then fdrop fdrop Cnt ! Cnt @ 64 < if \ Leave points in Mandel set black \ otherwise plot a pretty color i 300 j - Cnt @ $0F and drop 2drop ( putpixel) \ Double speed by taking advantage i 300 j + Cnt @ $0F and drop 2drop ( putpixel) \ of symmetry about X axis then loop ( X ) loop ( Y ) ; : Mandel ( -- ) \ $6A SetVid \ Set 800x600x16 graphic screen ShowMandel \ key drop \ Wait for keystroke, so plot can be admired \ $03 SetVid \ Restore standard text display ; \ turnkey Mandel mand \ make executable