Created
July 2, 2011 19:18
-
-
Save sneeu/1061548 to your computer and use it in GitHub Desktop.
Generates a Mandelbrot fractal as a PNM image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Example http://www.flickr.com/photos/sneeu/5894879494/ | |
(ns mandel | |
(:require [clojure.string :as string])) | |
(defn mandel | |
"Generates a Mandelbrot image as a seq of seqs." | |
[size max-iteration scale depth] | |
(let [width (first size) | |
height (second size) | |
mandel-limit (fn mandel-limit [x0 y0 x y i] | |
(if (= i max-iteration) | |
0 | |
(if (> (- (* x x) (* y y)) 4) | |
(int (/ (* i depth) max-iteration)) | |
(mandel-limit x0 y0 (+ x0 (- (* x x) (* y y))) (+ y0 (* 2 x y)) (+ i 1))))) | |
mandel-point #(let [x0 (* (+ (/ width -2) %1) scale) | |
y0 (* (+ (/ height -2) %2) scale)] | |
(mandel-limit x0 y0 x0 y0 0)) | |
mandel-row (fn [x ys] (map #(mandel-point x %1) ys))] | |
(map #(mandel-row %1 (range 0 height)) (range 0 width)))) | |
(defn mandel-pnm | |
[size max-iteration scale depth] | |
(println (format "P2\n%d %d\n%d" (first size) (second size) depth)) | |
(println (string/join "\n" (map #(string/join " " %1) (mandel size max-iteration scale depth))))) | |
(mandel-pnm '(800 800) 200 0.005 255) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment