Skip to content

Commit

Permalink
[#15] Use looping control functions in mandelbrot example
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Nov 29, 2018
1 parent d253238 commit dbd3931
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/doodler/core.clje
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@

(defn start-loop
[]
(u/swap-var! *sketch* assoc :looping? true))
(u/swap-var! *sketch* assoc :looping? true)
(u/send-message (:pid *sketch*) :refresh))

(defmacro defsketch
[name & opts]
Expand Down
29 changes: 21 additions & 8 deletions src/doodler/examples/mandelbrot.clje
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
minx -2.5
miny -2.0
wh 4.0]
[(+ minx (* wh (/ x w)))
(+ miny (* wh (/ y h)))]))
#erl[(+ minx (* wh (/ x w)))
(+ miny (* wh (/ y h)))]))

(defn ->color
[n max-iters]
(int (* 255 (/ n max-iters))))
(erlang/div (* 255 n) max-iters))

(defn mandelbrot [x y max-iters]
(loop [a 0.0
Expand All @@ -35,10 +35,19 @@
(inc n)))))

(defn mandelbrot-point [x y w h max-iters]
(let [[i j] (->complex x y w h)
n (mandelbrot i j max-iters)
color (->color n max-iters)]
[x y color]))
(let* [#erl[i j] (->complex x y w h)
n (mandelbrot i j max-iters)
color (->color n max-iters)]
#erl[x y color]))

(defn on-mouse-pressed [state handler]
(if-not (d/looping?)
(do
(d/start-loop)
(d/background 0)
{:points (points)
:m-points nil})
state))

(defn setup
[]
Expand All @@ -48,7 +57,10 @@

(defn update-state
[{points :points :as state}]
(when points
(if-not points
(do
(d/no-loop)
state)
(let [max-iters 100
w (d/width)
h (d/height)]
Expand All @@ -73,6 +85,7 @@
:size [300 300]
:setup setup
:update update-state
:mouse-pressed on-mouse-pressed
:draw draw
:bgcolor [0]
:features [:keep-on-top]
Expand Down
11 changes: 7 additions & 4 deletions src/doodler/sketch.clje
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
:frame-delay frame-period
:current-frame-rate default-frame-rate

:looping? true
:frame-count 0
:epoch (u/time-mark)
:p-time-mark (u/time-mark)
Expand Down Expand Up @@ -280,10 +281,12 @@
refresh
u/noreply))
([#erl[:draw time-mark] sketch]
(-> sketch
(on-draw (:draw-fn sketch))
(next-refresh time-mark)
u/noreply))
(u/noreply
(if (:looping? sketch)
(-> sketch
(on-draw (:draw-fn sketch))
(next-refresh time-mark))
sketch)))
([wx sketch]
(u/noreply sketch)))

Expand Down

0 comments on commit dbd3931

Please sign in to comment.