The Mandelbrot set with complex numbers

The same set as on the page The Mandelbrot set, computed as in mathematics with complex numbers: {z ← z ² + c} while {(magnitude z) ≤ 2} (mandelbrot+.rkt, mandelbrot-complex-canvas+.rkt).

JavaScript has no complex numbers: RacketScript gives them with its numbers of Scheme (the option --scheme-numbers), about 200 times slower than the numbers of JavaScript. So the image is smaller (320 × 224 points, shown twice as large) and it takes about ten seconds or more: please wait. In Racket and DrRacket the complex numbers are fast (mandelbrot-complex+.rkt draws the set with characters). Back

computing ... (about ten seconds or more)

mandelbrot-complex-canvas+.rkt

#lang reader SRFI-105

;; The Mandelbrot set with complex numbers (z ← z ² + c) drawn in a canvas of a web page , with RacketScript.

;; Author: Damien Mattei

;; For a web browser only (see mandelbrot-canvas+.rkt , the same with two reals). The complex numbers need the numbers
;; of Scheme of RacketScript , about 200 times slower than the numbers of JavaScript : a smaller image.
;;
;;   racks --scheme-numbers -d js mandelbrot-complex-canvas+.rkt
;; then the page mandelbrot-complex.html , served by a web server (see build.sh) : it has a canvas with the id
;; mandelbrot and a paragraph with the id info.

(require Scheme+
         (rename-in racketscript/interop ($> js-call)) ; (js-call object (method argument ...)) : object.method(argument ...)
         "mandelbrot+.rkt")

{page ⥆ ($ 'document)} ; the document of the page (not named document : RacketScript keeps the name , and the
                       ; variable would hide the document of JavaScript)
{canvas ⥆ (js-call page (getElementById ($/str "mandelbrot")))}
{context ⥆ (js-call canvas (getContext ($/str "2d")))}
{width ⥆ ($ canvas 'width)}
{height ⥆ ($ canvas 'height)}
{image ⥆ (js-call context (createImageData width height))}
{pixels ⥆ ($ image 'data)} ; red , green , blue , opacity of each pixel , from 0 to 255

{max-iterations ⥆ 50}

;; the rectangle of the complex plane , with the proportions of the canvas
{x-min ⥆ -2.2}
{x-max ⥆ 0.8}
{y-max ⥆ (x-max - x-min) · height / width / 2}

{start ⥆ (js-call ($ 'Date) (now))}

(for ({row ⥆ 0} {row < height} {row ← row + 1})
  {cy ⥆ y-max - row · 2 · y-max / height}
  (for ({column ⥆ 0} {column < width} {column ← column + 1})
    {c ⥆ (make-rectangular {x-min + column · (x-max - x-min) / width} cy)} ; the point c of the complex plane
    {n ⥆ (mandelbrot-iterations-complex c max-iterations)}
    {t ⥆ n / max-iterations} ; from 0 to 1 , 1 in the set (black)
    {i ⥆ 4 · (row · width + column)}
    ($/:= ($ pixels i) (exact-round {9 · (1 - t) · t ³ · 255}))
    ($/:= ($ pixels {i + 1}) (exact-round {15 · (1 - t) ² · t ² · 255}))
    ($/:= ($ pixels {i + 2}) (exact-round {8.5 · (1 - t) ³ · t · 255}))
    ($/:= ($ pixels {i + 3}) 255)))

(js-call context (putImageData image 0 0))

{elapsed ⥆ (js-call ($ 'Date) (now)) - start}
($/:= ($ (js-call page (getElementById ($/str "info"))) 'textContent)
      ($/str (string-append (number->string width) " × " (number->string height) " points , "
                            (number->string max-iterations) " iterations at most , with complex numbers , computed in "
                            (number->string elapsed) " ms")))

mandelbrot+.rkt

#lang reader SRFI-105

;; The Mandelbrot set : the number of iterations of z ← z ² + c before |z| > 2 , for a point c = cx + i·cy of the
;; complex plane. A module used by mandelbrot-ascii+.rkt (a drawing with characters , in DrRacket , Racket or
;; Node.js) and by mandelbrot-canvas+.rkt (a drawing in a canvas of a web page , with RacketScript).

;; Author: Damien Mattei

;; z = x + i·y : z ² + c = (x ² - y ² + cx) + i·(2·x·y + cy) , |z| > 2 when x ² + y ² > 4
;; (the squares are written x · x : the same floats in Racket and in JavaScript , then the same drawings)

(provide mandelbrot-iterations
         mandelbrot-iterations-complex)

(require Scheme+)


;; the number of iterations before |z| > 2 , or max-iterations when z stays in the disk (c is in the set)
;; (a while and not a return in a for : with RacketScript a return is made with an exception , slow when it is
;; done for each point , about 40 µs in Node.js)
(define (mandelbrot-iterations cx cy max-iterations)
  {x ⥆ 0.0}
  {y ⥆ 0.0}
  {n ⥆ 0}
  (while {(n < max-iterations) and (x · x + y · y ≤ 4.0)}
    {x-next ⥆ x · x - y · y + cx}
    {y ← 2.0 · x · y + cy}
    {x ← x-next}
    {n ← n + 1})
  n)


;; the same with complex numbers , as in mathematics : z ← z ² + c
;; (with RacketScript the complex numbers need the numbers of Scheme , --scheme-numbers , about 200 times slower than
;; the numbers of JavaScript ; in Racket this version is faster than the one with two reals)
(define (mandelbrot-iterations-complex c max-iterations)
  {z ⥆ 0}
  {n ⥆ 0}
  (while {(n < max-iterations) and ((magnitude z) ≤ 2)}
    {z ← z ² + c}
    {n ← n + 1})
  n)