An ASCII renderer of the Mandelbrot set in Yip!

............,,,,,,,,,,,:::::::::::::::::::::::::::;;;;;;;+%*+++;;;;;;::::::,,,,,
..........,,,,,,,,::::::::::::::::::::::::::;;;;;;;;;;+++*x:*#xx#*+;;;;;;:::::::
........,,,,,,::::::::::::::::::::::::::;;;;;;;;;;;+++++*x## x+@#x*++;;;;;;;::::
......,,,,,::::::::::::::::::::::::::;;;;;;;;;;;++++**x +.*;  x:,#%*+++++;;;;;;:
.....,,,,::::::::::::::::::::::::;;;;;;;;;;;++*****xxx#%,;       +%#x****++++;;;
....,,,::::::::::::::::::::::;;;;;;;;+++++**x@. : @@#%; %:;.   %.+#@,::##x##.@*+
...,,::::::::::::::::::::;;;;;+++++++++****x#%#   %                     *   :.%*
..,,::::::::::::::::;;;++*+++++++++*****x##+%,:                           @; #x*
..:::::::::;;;;;;;+++**#:%#xxx#@;#xxxxx##@+*                                +.;,
.,:::;;;;;;;;;;+++++**x#% :;:*+x@ +@. @@ ,                                   : #
.:;;;;;;;;;;+++++++*xx# :x             *+                                    . #
.;;;;;;;;+*****xxx%@x,:+%               .                                    ,x*
;*, ;:#  +#@ ,x;.                                                        +; %x**
.;;;;;;;;+*****xxx%@x,:+%               .                                    ,x*
.:;;;;;;;;;;+++++++*xx# :x             *+                                    . #
.,:::;;;;;;;;;;+++++**x#% :;:*+x@ +@. @@ ,                                   : #
..:::::::::;;;;;;;+++**#:%#xxx#@;#xxxxx##@+*                                +.;,
..,,::::::::::::::::;;;++*+++++++++*****x##+%,:                           @; #x*
...,,::::::::::::::::::::;;;;;+++++++++****x#%#   %                     *   :.%*
....,,,::::::::::::::::::::::;;;;;;;;+++++**x@. : @@#%; %:;.   %.+#@,::##x##.@*+
.....,,,,::::::::::::::::::::::::;;;;;;;;;;;++*****xxx#%,;       +%#x****++++;;;
......,,,,,::::::::::::::::::::::::::;;;;;;;;;;;++++**x + *;  x:,#%*+++++;;;;;;:
........,,,,,,::::::::::::::::::::::::::;;;;;;;;;;;+++++*x## x+@#x*++;;;;;;;::::
..........,,,,,,,,::::::::::::::::::::::::::;;;;;;;;;;+++*x:*#xx#*+;;;;;;:::::::

First we import the complex arithmetic module from the standard library, and set some constants for the program.

:extra/complex use

:= X-START -2.0 ;
:= Y-START -1.12 ;

:= X-DELTA 0.030875 ;
:= Y-DELTA 0.093333333 ;

:= MAX-ITERATIONS 100 ;

The main block sets up a loop for a 80x24 canvas and calls the mandelbrot function while maintaining the x_0 and y_0 values updated.

Y-START >y_0
24 .times: # Y
  [ X-START >x_0
    80 .times: # X
      [ complex/zero >z
        x_0. y_0. complex/new >c
        0 >iter
        mandelbrot plot
        x_0> X-DELTA + >x_0
        z/ c/
      ] ;
    x_0/
    y_0> Y-DELTA + >y_0
    nl
  ] ;

The plot word is used to plot an ASCII character given the iterations taken in the iterative algorithm used to draw the Mandelbrot set.

: plot # ( iter: -- )
  iter> dup MAX-ITERATIONS < .if:
    [ " .,:;+*x#%@" >ramp
      ramp. .length fx/ swap drop
      ramp> .nth string/from-byte ]
    [ drop " " ] ;
  display ;

The mandelbrot word runs the iterative algorithm used to draw the Mandelbrot set.

The z: notation for the stack effects is used to denote that it takes a value named z from the z stack.

: mandelbrot \( z: c: iter: -- z: c: iter: )
  z. dup .conj .mul .re 4 <=
  iter. MAX-ITERATIONS < and
  .when:
    [ z> dup .mul c. swap .add >z
      iter> 1 + >iter
      mandelbrot
    ] ;
  ;

:site use render-self: {:page-title "An ASCII renderer of the Mandelbrot set in Yip!"};