#!/usr/bin/env newlisp ; Cormullion http://unbalanced-parentheses.nfshost.com/ (seed (date-value)) ; michael's complex context (define (Class:Class) (cons (context) (args))) (new Class 'Complex) (define (Complex:rad c) (sqrt (add (pow (c 1) ) (pow (c 2))))) (define (Complex:add a b) (Complex (add (a 1) (b 1)) (add (a 2) (b 2)))) (define (Complex:mul a b) (let (a.re (a 1) a.im (a 2) b.re (b 1) b.im (b 2)) (Complex (sub (mul a.re b.re) (mul a.im b.im)) (add (mul a.re b.im) (mul a.im b.re))))) ; didi onograph code, reduced (context 'PPM) (set 'cmax 255) (set 'cmin 0) (set 'cdelta (/ (sub cmax cmin) 10 )) ; "/" only for int else "div" (set 'white (dup cmax 3 )) (set 'black (dup cmin 3 )) (define (ppm mrgb) (pack (dup "b" (length mrgb)) mrgb)) (set 'bkcolor (ppm black)) (set 'mcolor (ppm white)) (define (create w h) (set 'width w 'height h) (set 'bmparray (array (* w h) (list bkcolor)))) (define (limit rgbx) (if (> rgbx cmax) cmax (if (< rgbx cmin) cmin rgbx ))) (define (plot x y colour) (setf (bmparray (+ x (* y width))) (ppm colour))) (define (save-as fnm) (write-file (string fnm ".ppm") (append "P6\n" (string width) " " (string height) "\n255\n")) ;; convert bitmap to string and append to ppm-file (append-file (string fnm ".ppm") (join (array-list bmparray)))) (context 'Mandel) (seed (date-value)) (set 'colour-map '((0 0 0) (1 1 1) (2 2 2))) (for (i 0 253) (push (list (rand 200) (rand 200) (rand 200)) colour-map -1)) (define (do-cell x y col) (local (colour) (set 'colour (colour-map col)) (PPM:plot x y colour))) (define (escape x y) (set 'z (Complex x y) 'c 250 'a z) (while (and (< (abs (:rad (set 'z (:add (:mul z z) a)))) 2) (> (dec c) 0))) c) (define (draw w h min-x max-x min-y max-y) (local (x-pos y-pos) (for (y 0 (- w 1)) (for (x 0 (- h 1)) (set 'x-pos (add min-x (mul (sub max-x min-x) (div x w)))) (set 'y-pos (add min-y (mul (sub max-y min-y) (div y h)))) (do-cell x y (escape x-pos y-pos)))))) (context MAIN) (PPM:create 70 70) ; width/height of image ; these next 4 values should define an area with the same aspect as the width/height just given... (set 'min-x -0.5 'max-x -0.4) (set 'min-y -0.65 'max-y -0.55) (Mandel:draw PPM:width PPM:height min-x max-x min-y max-y) (PPM:save-as (string (env "HOME") "/mandel")) ; convert to PNG and open ; works on MacOS X :) (exec (string {/usr/local/bin/convert } (string (env "HOME") "/mandel.ppm") { } (string (env "HOME") "/mandel.png"))) (exec (string "open " (string (env "HOME") "/mandel.png"))) (exit)