This file contains 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
/* | |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. | |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
* | |
* This code is free software; you can redistribute it and/or modify it | |
* under the terms of the GNU General Public License version 2 only, as | |
* published by the Free Software Foundation. Oracle designates this | |
* particular file as subject to the "Classpath" exception as provided | |
* by Oracle in the LICENSE file that accompanied this code. | |
* |
This file contains 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
(defn render-image [image] | |
(let [out (ByteArrayOutputStream.)] | |
(do (ImageIO/write image "png" out) (ByteArrayInputStream. (.toByteArray out))))) | |
; ":use" needed for compojure.response | |
(extend-protocol Renderable | |
BufferedImage | |
(render [image _] | |
(-> (ring.util.response/response (render-image image)) | |
(ring.util.response/content-type "image/png")))) |
This file contains 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
(defn- image-mandel-doseq | |
([[dx dy w h m] limit] | |
(let [image (BufferedImage. w h BufferedImage/TYPE_INT_RGB)] | |
(image-mandel-doseq dx dy m image limit))) | |
([dx dy m ^BufferedImage image limit] | |
(let [raster (.getRaster image)] | |
(doseq [[x y rate] m] | |
(.setPixel raster (int (- x dx)) (int (- y dy)) (ints (rgb (angle rate limit))))) | |
[dx dy image]))) |
This file contains 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
(defn image-mandel | |
([size a b limit] | |
(let [[_ _ ms] (mandelbrot-seqs 1 size a b limit) | |
[_ _ image] (image-mandel-doseq (first ms) limit)] | |
image)) | |
([n size a b limit] | |
(let [[w h ms] (mandelbrot-seqs n size a b limit) | |
image (BufferedImage. w h BufferedImage/TYPE_INT_RGB) | |
raster (.getRaster image)] | |
(dorun |
This file contains 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
(defn text-mandel [size a b limit] | |
(let [[w _ ms] (mandelbrot-seqs 1 size a b limit) | |
[_ _ _ _ m] (first ms) | |
s (map (fn [[_ _ i]] | |
(if (zero? i) " " (str (int (* 9 (/ i limit)))))) m)] | |
(println | |
(interpose \newline | |
(map #(apply str %) | |
(partition w s)))))) |
This file contains 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
(defn- ranges [l s] | |
(partition 2 1 [l] (range 0 l s))) |
This file contains 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
(defn- mandelbrot-seqs [n size a b limit] | |
(let [[lower upper] (normalize a b) | |
d (delta size lower upper) | |
[w h] (dimensions size lower upper)] | |
[w h | |
(for [[ly uy] (ranges h (int (/ h (min n h))))] | |
[ 0 ly w (- uy ly) (mandelbrot-seq (range 0 w) (range ly uy) lower d limit) ] | |
)])) |
This file contains 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
(defn- mandelbrot-seq [x-range y-range [lr li] d limit] | |
(for [y y-range :let [i (+ li (* y d))] | |
x x-range :let [r (+ lr (* x d))]] | |
[x y (mandelbrot r i limit)])) |
This file contains 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
(defn mandelbrot [c ^long limit] | |
(loop [rate limit | |
s (iterate #(+ (* % %) c) 0.0)] | |
(if (and (> rate 0) (< (abs (first s)) 2.0)) | |
(recur (dec rate) (rest s)) | |
rate))) |
This file contains 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
(defn mandelbrot [^double cr ^double ci ^long limit] | |
(loop [rate limit | |
zr 0.0 | |
zi 0.0] | |
(let [zr2 (* zr zr) | |
zi2 (* zi zi)] | |
(if (and (> rate 0) (< (+ zr2 zi2) 4.0)) | |
(recur (dec rate) (+ cr (- zr2 zi2)) (+ ci (* 2.0 (* zr zi))) ) | |
rate)))) |