github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

alexspurling / clojure-projects

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 2
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A home for my projects and experiments with Clojure — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Adding memoization to iter-colour 
alexspurling (author)
Tue Nov 24 14:31:26 -0800 2009
commit  9ad79d1dca48fefff43f143602019d9c045deb5e
tree    441a83d024b1f8a3f083e0e0db4c02be8288ccd0
parent  d8771335f63a03df22b2e83a3132bb80107f27f4
clojure-projects / Mandelbrot / src / fast-mandelbrot.clj Mandelbrot/src/fast-mandelbrot.clj
100644 107 lines (92 sloc) 3.725 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(import '(javax.swing JFrame JPanel)
        '(java.awt Color Dimension Graphics)
        '(java.awt.image BufferedImage WritableRaster))
 
(defn mandelformula [x0 y0 max-iters]
  "Applies the mandelbrot formula until max-iters iterations
are reached, or the magnitude of Z exceeds 2"
  (let [x0 (double x0)
        y0 (double y0)
        max-iters (int max-iters)]
    (loop [x (double x0)
           y (double y0)
           n (int 0)]
      (if (== n max-iters)
        n
        (let [mag (+ (* x x) (* y y))]
          (if (>= mag (double 4))
            n
            (let [new-x (+ x0 (- (* x x) (* y y)))
                  new-y (+ y0 (* (double 2) (* x y)))]
              (recur new-x new-y (inc n)))))))))
 
;; Colours used to draw the set
(def *grad-colour-a* [255, 255, 0]) ;yellow
(def *grad-colour-b* [0, 0, 255]) ;blue
(def *set-colour* [0, 0, 0]) ;black
 
(defn grad-colour
  "Returns the colour that is the given fraction of the way between
the first and second colours given. Returns as a vector of three
integers between 0 and 255."
  [colA colB frac]
  (vec (map #(+ (* frac (- %2 %1)) %1) colA colB)))
 
(defn iter-colour
  "Returns the colour needed to paint a point with the given number
of iterations"
  [num-iters max-iters]
  (grad-colour *grad-colour-a* *grad-colour-b*
    (/ (double num-iters) max-iters)))
 
(def iter-colour (memoize iter-colour))
 
(defn coord-colour
  "Returns a colour for which to draw the given coordinate. If the coordinate
is within the mandelbrot set, black is returned. Otherwise, a colour within
a gradient is given based on the number of iterations of the mandelbrot set
that have been evaluated."
  [[xcoord ycoord] max-iters]
  (let [num-iters (mandelformula xcoord ycoord max-iters)]
    (if (= max-iters num-iters)
      *set-colour*
      (iter-colour num-iters max-iters))))
 
(defn get-coord
  "Returns the coordinates of the given pixel in the complex plane"
  [x y xstart ystart xsize ysize width height]
  [(+ xstart (* (/ x width) xsize))
   (+ ystart (* (/ y height) ysize))])
 
(defn get-pixels [width height]
  "Returns a sequence of vectors representing pixels in a grid of
the given width and height"
  (for [y (range height) x (range width)]
     [x y]))
 
(defn render [xstart ystart xsize ysize width height max-iters #^WritableRaster wr]
  (doseq [pixel (get-pixels width height)]
    (let [[x y] pixel]
      (.setPixel wr (int x) (int y)
        (int-array
          (coord-colour
            (get-coord (double x) (double y) xstart ystart xsize ysize width height)
            max-iters))))))
 
(defn get-img [width height]
  (BufferedImage. width height (BufferedImage/TYPE_INT_RGB)))
 
(defn get-panel [width height img]
  (proxy [JPanel] [] (paint [g] (.drawImage g img 0 0 (Color/red) nil))))
 
(defn construct-frame [width height panel]
  "Creates and displays a JFrame of the given dimensions with
the panel added to it"
  (let [frame (JFrame.)]
      (.setPreferredSize panel (Dimension. width height))
      (doto frame
        (.add panel)
        .pack
        (.setLocationRelativeTo nil)
        .show)))
 
(defn mandelbrot [xstart ystart xsize ysize width height max-iters]
  "Returns a function to render the mandelbrot set with the
given parameters on a frame"
  (let [img (get-img width height)
        panel (get-panel width height img)
        wr (.getRaster img)]
    (construct-frame width height panel)
    (fn []
      (do
        (render xstart ystart xsize ysize width height max-iters wr)
        (.repaint panel)))))
 
(def my-mandelbrot (mandelbrot -2 -1.25 3 2.5 600 500 50))
 
(future (my-mandelbrot))
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server