Edit this page

8 Line Mandelbrot

The forth code below prints image of the Mandelbrot Set in your terminal using ascii characters.

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

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 bl ;
: left->right -1.5e 80 0 do fover fover pixel emit 0.026e f+ loop fdrop ;
: top->bottom -1e 40 0 do left->right cr 0.05e f+ loop fdrop ;
top->bottom bye

See Also

forth code



Edit this page · home ·