Skip to content

Commit

Permalink
Chapter 0 Example 6 and Exercise 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed May 29, 2024
1 parent 0517e07 commit c4ad68f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
24 changes: 24 additions & 0 deletions notebooks/chapter_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,27 @@ of being less than $r2$, thus more likely to be selected.
(slurp "src/noc/chapter_0_6e.cljs")
(show-sketch :c0.6e)
```


## [Example 0.6: A Perlin Noise Walker](https://natureofcode.com/random/#example-06-a-perlin-noise-walker)

```clojure
^{::clerk/no-cache true ::clerk/viewer clerk/code}
(slurp "src/noc/chapter_0_6.cljs")
(show-sketch :c0.6)
```


## [Exercise 0.7: A Perlin Noise Walker - Step Size](https://natureofcode.com/random/#exercise-07)

```clojure
^{::clerk/no-cache true ::clerk/viewer clerk/code}
(slurp "src/noc/chapter_0_7e.cljs")
(show-sketch :c0.7e)
```

This one is rather different than the others. It spends a lot of time hovering
around the edges, so I implemented movement wrapping so the paths generated by
the perlin noise are more apparent.

Toggle between movement wrapping and canvas-constrained movement by clicking the checkbox.
32 changes: 32 additions & 0 deletions src/noc/chapter_0_6.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns noc.chapter-0-6
(:require
[goog.string :as gstring]
[goog.string.format]
[quil.core :as q]))

(def size [640 240])

(defn init-state [{:keys [width height]}]
{:walker {:x nil
:y nil
:tx 0
:ty 10000}})

(defn setup! [_]
(q/background 255))

(defn step-walker [width height {:keys [tx ty] :as walker}]
{:x (q/map-range (q/noise tx) 0 1 0 width)
:y (q/map-range (q/noise ty) 0 1 0 height)
:tx (+ tx 0.01)
:ty (+ ty 0.01)})

(defn tick [{:keys [width height] :as state}]
(update state :walker (partial step-walker width height)))

(defn draw! [{:keys [walker ui]}]
(q/stroke-weight 2)
(q/fill 127)
(q/stroke 0)
(when (:x walker)
(q/ellipse (:x walker) (:y walker) 48 48)))
53 changes: 53 additions & 0 deletions src/noc/chapter_0_7e.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns noc.chapter-0-7e
(:require
[goog.string :as gstring]
[goog.string.format]
[quil.core :as q]))

(def size [640 240])

(defn init-state [{:keys [width height]}]
{:walker {:x (quot width 2)
:y (quot height 2)
:tx 0
:ty 10000}
:ui {:wrap {:type :checkbox :checked? true :label " Wrap Movement?"}}})

(defn setup! [_]
(q/background 255))

(defn tick [{:keys [width height ui] :as state}]
(letfn [(step-walker [{:keys [x y tx ty]}]
(let [max-step 10
stepx (q/map-range (q/noise tx) 0 1 (- max-step) max-step)
stepy (q/map-range (q/noise ty) 0 1 (- max-step) max-step)]
{:x (+ x stepx)
:y (+ y stepy)
:tx (+ tx 0.01)
:ty (+ ty 0.01)}))
(wrap [walker]
(-> walker
(update :x #(cond
(>= % width) 0
(< % 0) width
:else %))
(update :y #(cond
(>= % height) 0
(< % 0) height
:else %))))

(constrain [walker]
(-> walker
(update :x #(q/constrain % 0 (- width 1)))
(update :y #(q/constrain % 0 (- height 1)))))]
(update state :walker (comp
(if (get-in ui [:wrap :checked?])
wrap
constrain) step-walker))))

(defn draw! [{:keys [walker ui]}]
(q/stroke-weight 2)
(q/fill 127)
(q/stroke 0)
(when (:x walker)
(q/ellipse (:x walker) (:y walker) 48 48)))
8 changes: 6 additions & 2 deletions src/noc/sketch.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
[noc.chapter-0-4e :as c0.4e]
[noc.chapter-0-5e :as c0.5e]
[noc.chapter-0-5 :as c0.5]
[noc.chapter-0-6e :as c0.6e]))
[noc.chapter-0-6e :as c0.6e]
[noc.chapter-0-6 :as c0.6]
[noc.chapter-0-7e :as c0.7e]))

(def sketches {:walker (sketch-> c0.1)
:rand-dist (sketch-> c0.2)
Expand All @@ -22,7 +24,9 @@
:c0.4e (sketch-> c0.4e)
:c0.5e (sketch-> c0.5e)
:c0.5 (sketch-> c0.5)
:c0.6e (sketch-> c0.6e)})
:c0.6e (sketch-> c0.6e)
:c0.6 (sketch-> c0.6)
:c0.7e (sketch-> c0.7e)})

(defn load-sketch [s]
(when-let [sk (get sketches s)]
Expand Down

0 comments on commit c4ad68f

Please sign in to comment.