Edit this page

8 Line Mandelbrot

the forth code below generates a PPM image of the Mandelbrot Set.

To run it, paste the 8 lines below into mandel.fs, and run this:

$ gforth mandel.fs > mandel.ppm; xview mandel.ppm

Note: This code was written to show that forth can create a mandelbrot in 8 lines. Under other circumstances the code would be much more readable and factored better.


fvariable ci fvariable c fvariable zi fvariable z
: >2? z f@ fdup f* zi f@ fdup f* f+ 4.0e f> ;
: nextr z f@ fdup f* zi f@ fdup f* f- c f@ f+ ;
: nexti z f@ zi f@ f* 2.0e f* ci f@ f+ ;
: pixel c f! ci f! 0e z f! 0e zi f! 150 50 do nextr nexti zi f! z f! >2? if i unloop exit then loop 0 ;
: left->right -1.5e 400 0 do fover fover pixel emit 0.005e f+ loop fdrop ;
: top->bottom -1e 400 0 do left->right 0.005e f+ loop fdrop ;
.( P5) cr .( 400 400) cr .( 255) cr top->bottom bye

See Also

forth code

forth

gforth

Edit this page · home ·