Skip to content

Commit 7d8869e

Browse files
committed
Chapter 1 Exercise 3
1 parent 512f031 commit 7d8869e

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

notebooks/chapter_1.md

+16
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ What's important is that we introduce the [`[thi.ng.geom.vector]`](https://cljdo
4040
Why did I choose to pull in `thi.ng/geom` rather than use p5.js's `createVector` and other associated functions?
4141

4242
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.
43+
44+
45+
## Exercises 1.1 and 1.2
46+
47+
I am skipping these exercises as they don't add much to what we did in Example 1
48+
49+
## [Exercise 1.3: Bouncing Sphere](https://natureofcode.com/vectors/#exercise-13)
50+
51+
52+
```clojure
53+
^{::clerk/no-cache true ::clerk/viewer clerk/code}
54+
(slurp "src/noc/chapter_1_3e.cljs")
55+
(show-sketch :c1.3e)
56+
```
57+
58+
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.

src/noc/chapter_1_3e.cljs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
(ns noc.chapter-1-3e
2+
(:require
3+
[thi.ng.math.core :as tm]
4+
[thi.ng.geom.vector :as v]
5+
[quil.core :as q]))
6+
7+
(def size [640 300])
8+
9+
(defn init-state [{:keys [width height] :as state}]
10+
{:angle 0.0
11+
:pos (v/vec3 0 0 0)
12+
:vel (v/vec3 2 2.5 3)
13+
:thresh 75})
14+
15+
(defn setup! [{:keys [width height]}]
16+
(q/background 255)
17+
(q/no-fill))
18+
19+
(defn tick [{:keys [angle pos vel thresh]}]
20+
(let [[x y z] (tm/+ pos vel)
21+
[vx vy vz] vel
22+
angle (+ angle 0.05)
23+
vx (if (> (abs x) thresh) (* vx -1) vx)
24+
vy (if (> (abs y) thresh) (* vy -1) vy)
25+
vz (if (> (abs z) thresh) (* vz -1) vz)]
26+
{:pos (tm/+ pos vel)
27+
:thresh thresh
28+
:vel (v/vec3 vx vy vz)
29+
:angle angle}))
30+
31+
(defn draw! [{:keys [pos angle]}]
32+
(let [[x y z] pos]
33+
(q/background 255)
34+
(q/directional-light 255 255 255 1 1 -1)
35+
(q/stroke 0)
36+
(q/box 200)
37+
(q/push-matrix)
38+
(q/translate x y z)
39+
(q/rotate-x angle)
40+
(q/rotate-y angle)
41+
(q/stroke 0)
42+
(q/sphere 30)
43+
(q/pop-matrix)))

src/noc/sketch.cljs

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
[noc.chapter-0-9e :as c0.9e]
2121
[noc.chapter-0-10e :as c0.10e]
2222
[noc.chapter-1-1 :as c1.1]
23-
[noc.chapter-1-2 :as c1.2]))
23+
[noc.chapter-1-2 :as c1.2]
24+
[noc.chapter-1-3e :as c1.3e]))
2425

2526
(def sketches
2627
{:walker (sketch-> c0.1)
@@ -39,7 +40,8 @@
3940
:c0.9e (sketch-> c0.9e)
4041
:c0.10e (sketch-3d-> c0.10e)
4142
:c1.1 (sketch-> c1.1)
42-
:c1.2 (sketch-> c1.2)})
43+
:c1.2 (sketch-> c1.2)
44+
:c1.3e (sketch-3d-> c1.3e)})
4345

4446
(defn load-sketch [s]
4547
(when-let [sk (get sketches s)]
@@ -148,14 +150,12 @@
148150

149151
(defn show-sketch [adjust-frame {:keys [init setup tick draw size] :as opts} el]
150152
{:applet (apply q/sketch (apply concat
151-
(doto
152-
(-> opts
153-
(assoc :middleware [m/fun-mode])
154-
(assoc :host el)
155-
(assoc :update (partial tick-wrapper tick))
156-
(assoc :setup (partial setup-wrapper (partial adjust-frame el) init setup))
157-
(assoc :draw (partial draw-wrapper draw)))
158-
prn)))
153+
(-> opts
154+
(assoc :middleware [m/fun-mode])
155+
(assoc :host el)
156+
(assoc :update (partial tick-wrapper tick))
157+
(assoc :setup (partial setup-wrapper (partial adjust-frame el) init setup))
158+
(assoc :draw (partial draw-wrapper draw)))))
159159
:sketch-name (:sketch-name opts)
160160
:opts opts
161161
:el el})

0 commit comments

Comments
 (0)