Skip to content

Commit

Permalink
Chapter 1 Exercise 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramblurr committed Jun 4, 2024
1 parent 512f031 commit 7d8869e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
16 changes: 16 additions & 0 deletions notebooks/chapter_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ What's important is that we introduce the [`[thi.ng.geom.vector]`](https://cljdo
Why did I choose to pull in `thi.ng/geom` rather than use p5.js's `createVector` and other associated functions?

Well, Quil doesn't expose these functions because the P5.js and Processing APIs for vectors is not at all compatible. Furthermore I want the code in this notebook to be more or less Clojure and Clojurescript compatible. The `thi.ng/geom` library provides a bunch of tools that we will use in the future.


## Exercises 1.1 and 1.2

I am skipping these exercises as they don't add much to what we did in Example 1

## [Exercise 1.3: Bouncing Sphere](https://natureofcode.com/vectors/#exercise-13)


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

This was more an exercise in 3d than vectors, haha! I fake the boundary condition for the sphere, but it looks more or less right.
43 changes: 43 additions & 0 deletions src/noc/chapter_1_3e.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns noc.chapter-1-3e
(:require
[thi.ng.math.core :as tm]
[thi.ng.geom.vector :as v]
[quil.core :as q]))

(def size [640 300])

(defn init-state [{:keys [width height] :as state}]
{:angle 0.0
:pos (v/vec3 0 0 0)
:vel (v/vec3 2 2.5 3)
:thresh 75})

(defn setup! [{:keys [width height]}]
(q/background 255)
(q/no-fill))

(defn tick [{:keys [angle pos vel thresh]}]
(let [[x y z] (tm/+ pos vel)
[vx vy vz] vel
angle (+ angle 0.05)
vx (if (> (abs x) thresh) (* vx -1) vx)
vy (if (> (abs y) thresh) (* vy -1) vy)
vz (if (> (abs z) thresh) (* vz -1) vz)]
{:pos (tm/+ pos vel)
:thresh thresh
:vel (v/vec3 vx vy vz)
:angle angle}))

(defn draw! [{:keys [pos angle]}]
(let [[x y z] pos]
(q/background 255)
(q/directional-light 255 255 255 1 1 -1)
(q/stroke 0)
(q/box 200)
(q/push-matrix)
(q/translate x y z)
(q/rotate-x angle)
(q/rotate-y angle)
(q/stroke 0)
(q/sphere 30)
(q/pop-matrix)))
20 changes: 10 additions & 10 deletions src/noc/sketch.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
[noc.chapter-0-9e :as c0.9e]
[noc.chapter-0-10e :as c0.10e]
[noc.chapter-1-1 :as c1.1]
[noc.chapter-1-2 :as c1.2]))
[noc.chapter-1-2 :as c1.2]
[noc.chapter-1-3e :as c1.3e]))

(def sketches
{:walker (sketch-> c0.1)
Expand All @@ -39,7 +40,8 @@
:c0.9e (sketch-> c0.9e)
:c0.10e (sketch-3d-> c0.10e)
:c1.1 (sketch-> c1.1)
:c1.2 (sketch-> c1.2)})
:c1.2 (sketch-> c1.2)
:c1.3e (sketch-3d-> c1.3e)})

(defn load-sketch [s]
(when-let [sk (get sketches s)]
Expand Down Expand Up @@ -148,14 +150,12 @@

(defn show-sketch [adjust-frame {:keys [init setup tick draw size] :as opts} el]
{:applet (apply q/sketch (apply concat
(doto
(-> opts
(assoc :middleware [m/fun-mode])
(assoc :host el)
(assoc :update (partial tick-wrapper tick))
(assoc :setup (partial setup-wrapper (partial adjust-frame el) init setup))
(assoc :draw (partial draw-wrapper draw)))
prn)))
(-> opts
(assoc :middleware [m/fun-mode])
(assoc :host el)
(assoc :update (partial tick-wrapper tick))
(assoc :setup (partial setup-wrapper (partial adjust-frame el) init setup))
(assoc :draw (partial draw-wrapper draw)))))
:sketch-name (:sketch-name opts)
:opts opts
:el el})

0 comments on commit 7d8869e

Please sign in to comment.