Computed by your browser: mandelbrot+.rkt (the computation) and mandelbrot-canvas+.rkt (the drawing in the canvas), Scheme+ programs compiled to JavaScript by RacketScript. In DrRacket, mandelbrot-ascii+.rkt draws it with characters. Back
computing ...
#lang reader SRFI-105 ;; The Mandelbrot set drawn in a canvas of a web page , with RacketScript. ;; Author: Damien Mattei ;; For a web browser only : this program uses the JavaScript of the page (the canvas) through the interoperability ;; of RacketScript (racketscript/interop : $ , $/:= , $/str , and $> renamed js-call : Scheme+ has its own $>) , so it ;; does not run in DrRacket or Racket ; the computation itself , mandelbrot+.rkt , runs everywhere (see ;; mandelbrot-ascii+.rkt). ;; ;; racks -d js mandelbrot-canvas+.rkt ;; then the page mandelbrot.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 ⥆ 256} ;; 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}) {cx ⥆ x-min + column · (x-max - x-min) / width} {n ⥆ (mandelbrot-iterations cx cy max-iterations)} {t ⥆ n / max-iterations} ; from 0 to 1 , 1 in the set (black) {i ⥆ 4 · (row · width + column)} ($/:= ($ pixels i) {9 · (1 - t) · t ³ · 255}) ($/:= ($ pixels {i + 1}) {15 · (1 - t) ² · t ² · 255}) ($/:= ($ pixels {i + 2}) {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 , computed in " (number->string elapsed) " ms")))
#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)