Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Created January 2, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/f4d4603449db5868d1a98ddad617671a to your computer and use it in GitHub Desktop.
Save porglezomp/f4d4603449db5868d1a98ddad617671a to your computer and use it in GitHub Desktop.
(let! *width* 128)
(let! *height* 128)
(struct cplx
real imag)
(let! real cplx/real)
(let! imag cplx/imag)
(defn (c/+ a b)
(cplx (+ (real a) (real b))
(+ (imag a) (imag b))))
(defn (c/- a b)
(cplx (- (real a) (real b))
(- (imag a) (imag b))))
(defn (c/* a b)
(cplx (- (* (real a) (real b))
(* (imag a) (imag b)))
(+ (* (imag a) (real b))
(* (real a) (imag b)))))
(defn (c/*s a s)
(cplx (* (real a) s) (* (imag a) s)))
(defn (c/square a)
(c/* a a))
(defn (c/len2 a)
(+ (* (real a) (real a))
(* (imag a) (imag a))))
(defn (mandelbrot- c z n)
(let! z (c/+ (c/square z) c))
(let! n (+ n 1))
(if (>= n 32) (return nil))
(if (>= (c/len2 z) 4) (return n))
(mandelbrot- c z n))
(defn (mandelbrot c)
(mandelbrot- c c 0))
(struct pixel
r g b)
(defn (pixel/write px)
(print (pixel/r px) (pixel/g px) (pixel/b px)))
(struct image
width
height
rows)
(defn (row/write row)
(list/map pixel/write row))
(defn (image/write img)
(print (quote P3))
(print (image/width img) (image/height img))
(print 255)
(list/map row/write (image/rows img)))
(defn (cplx/range- a step n)
(if (> n 0)
(cons a (cplx/range- (c/+ a step) step (- n 1)))))
(defn (cplx/range a b n)
(let! step-size (/ (- n 1)))
(let! diff (c/- b a))
(cplx/range- a (c/*s diff step-size) n))
(defn (count->color count)
(if count
(pixel (* 8 count) 0 0)
(pixel 0 0 0)))
(defn (make-row left)
(let! right (cplx (+ (real left) 3) (imag left)))
(list/map color-mandelbrot (cplx/range left right *width*)))
(defn (color-mandelbrot c)
(count->color (mandelbrot c)))
(image/write
(image *width* *height*
(list/map make-row (cplx/range (cplx (- 2.3) (- 1.5)) (cplx (- 2.3) 1.5) *height*))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment