Skip to content

Commit c4ad68f

Browse files
committed
Chapter 0 Example 6 and Exercise 7
1 parent 0517e07 commit c4ad68f

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

notebooks/chapter_0.md

+24
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,27 @@ of being less than $r2$, thus more likely to be selected.
182182
(slurp "src/noc/chapter_0_6e.cljs")
183183
(show-sketch :c0.6e)
184184
```
185+
186+
187+
## [Example 0.6: A Perlin Noise Walker](https://natureofcode.com/random/#example-06-a-perlin-noise-walker)
188+
189+
```clojure
190+
^{::clerk/no-cache true ::clerk/viewer clerk/code}
191+
(slurp "src/noc/chapter_0_6.cljs")
192+
(show-sketch :c0.6)
193+
```
194+
195+
196+
## [Exercise 0.7: A Perlin Noise Walker - Step Size](https://natureofcode.com/random/#exercise-07)
197+
198+
```clojure
199+
^{::clerk/no-cache true ::clerk/viewer clerk/code}
200+
(slurp "src/noc/chapter_0_7e.cljs")
201+
(show-sketch :c0.7e)
202+
```
203+
204+
This one is rather different than the others. It spends a lot of time hovering
205+
around the edges, so I implemented movement wrapping so the paths generated by
206+
the perlin noise are more apparent.
207+
208+
Toggle between movement wrapping and canvas-constrained movement by clicking the checkbox.

src/noc/chapter_0_6.cljs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns noc.chapter-0-6
2+
(:require
3+
[goog.string :as gstring]
4+
[goog.string.format]
5+
[quil.core :as q]))
6+
7+
(def size [640 240])
8+
9+
(defn init-state [{:keys [width height]}]
10+
{:walker {:x nil
11+
:y nil
12+
:tx 0
13+
:ty 10000}})
14+
15+
(defn setup! [_]
16+
(q/background 255))
17+
18+
(defn step-walker [width height {:keys [tx ty] :as walker}]
19+
{:x (q/map-range (q/noise tx) 0 1 0 width)
20+
:y (q/map-range (q/noise ty) 0 1 0 height)
21+
:tx (+ tx 0.01)
22+
:ty (+ ty 0.01)})
23+
24+
(defn tick [{:keys [width height] :as state}]
25+
(update state :walker (partial step-walker width height)))
26+
27+
(defn draw! [{:keys [walker ui]}]
28+
(q/stroke-weight 2)
29+
(q/fill 127)
30+
(q/stroke 0)
31+
(when (:x walker)
32+
(q/ellipse (:x walker) (:y walker) 48 48)))

src/noc/chapter_0_7e.cljs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(ns noc.chapter-0-7e
2+
(:require
3+
[goog.string :as gstring]
4+
[goog.string.format]
5+
[quil.core :as q]))
6+
7+
(def size [640 240])
8+
9+
(defn init-state [{:keys [width height]}]
10+
{:walker {:x (quot width 2)
11+
:y (quot height 2)
12+
:tx 0
13+
:ty 10000}
14+
:ui {:wrap {:type :checkbox :checked? true :label " Wrap Movement?"}}})
15+
16+
(defn setup! [_]
17+
(q/background 255))
18+
19+
(defn tick [{:keys [width height ui] :as state}]
20+
(letfn [(step-walker [{:keys [x y tx ty]}]
21+
(let [max-step 10
22+
stepx (q/map-range (q/noise tx) 0 1 (- max-step) max-step)
23+
stepy (q/map-range (q/noise ty) 0 1 (- max-step) max-step)]
24+
{:x (+ x stepx)
25+
:y (+ y stepy)
26+
:tx (+ tx 0.01)
27+
:ty (+ ty 0.01)}))
28+
(wrap [walker]
29+
(-> walker
30+
(update :x #(cond
31+
(>= % width) 0
32+
(< % 0) width
33+
:else %))
34+
(update :y #(cond
35+
(>= % height) 0
36+
(< % 0) height
37+
:else %))))
38+
39+
(constrain [walker]
40+
(-> walker
41+
(update :x #(q/constrain % 0 (- width 1)))
42+
(update :y #(q/constrain % 0 (- height 1)))))]
43+
(update state :walker (comp
44+
(if (get-in ui [:wrap :checked?])
45+
wrap
46+
constrain) step-walker))))
47+
48+
(defn draw! [{:keys [walker ui]}]
49+
(q/stroke-weight 2)
50+
(q/fill 127)
51+
(q/stroke 0)
52+
(when (:x walker)
53+
(q/ellipse (:x walker) (:y walker) 48 48)))

src/noc/sketch.cljs

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
[noc.chapter-0-4e :as c0.4e]
1313
[noc.chapter-0-5e :as c0.5e]
1414
[noc.chapter-0-5 :as c0.5]
15-
[noc.chapter-0-6e :as c0.6e]))
15+
[noc.chapter-0-6e :as c0.6e]
16+
[noc.chapter-0-6 :as c0.6]
17+
[noc.chapter-0-7e :as c0.7e]))
1618

1719
(def sketches {:walker (sketch-> c0.1)
1820
:rand-dist (sketch-> c0.2)
@@ -22,7 +24,9 @@
2224
:c0.4e (sketch-> c0.4e)
2325
:c0.5e (sketch-> c0.5e)
2426
:c0.5 (sketch-> c0.5)
25-
:c0.6e (sketch-> c0.6e)})
27+
:c0.6e (sketch-> c0.6e)
28+
:c0.6 (sketch-> c0.6)
29+
:c0.7e (sketch-> c0.7e)})
2630

2731
(defn load-sketch [s]
2832
(when-let [sk (get sketches s)]

0 commit comments

Comments
 (0)