Skip to content

Commit ecd9f9b

Browse files
committed
Chapter 1 Examples 3 and 4, plus Vector Math table reference
1 parent 7d8869e commit ecd9f9b

File tree

5 files changed

+168
-3
lines changed

5 files changed

+168
-3
lines changed

dev/user.clj

+16-1
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,20 @@
115115
[:_ui]
116116
#(dissoc % :foobar))
117117
(range 10)
118-
;;
118+
119+
()
120+
121+
(require '[thi.ng.math.core :as tm])
122+
(require '[thi.ng.geom.vector :as v])
123+
(require '[thi.ng.geom.core :as g])
124+
(let [v1 (v/vec2 2 2)
125+
v2 (v/vec2 1 1)
126+
v3 (v/vec2 3 4)]
127+
(= v3 (tm/+ v1 v2))
128+
(= (tm/* v1 4) (v/vec2 8.0 8.0))
129+
(= 5.0 (tm/mag (v/vec2 4 3)))
130+
(tm/+ (v/vec2 1 1) (v/vec2 1 1))
131+
(tm/normalize (v/vec2 2 3))
132+
(g/heading-xy (v/vec2 2 3)))
133+
;;
119134
)

notebooks/chapter_1.md

+81
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,84 @@ I am skipping these exercises as they don't add much to what we did in Example 1
5656
```
5757

5858
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.
59+
60+
61+
## More Vector Math
62+
63+
This section isn't an example or exercise, but this list of vector math related functions is very useful! The `thi.ng/geom` toolkit is very powerful, but doesn't have an easy to use getting started page.
64+
65+
So I'm reproducing the vector math function table from the book, but with functions from `thi.ng/geom`.
66+
67+
To create a vector use [`thi.ng.geom.vector/vec2`](https://cljdoc.org/d/thi.ng/geom/1.0.1/api/thi.ng.geom.vector#vec2) or [`thi.ng.geom.vector/vec3`](https://cljdoc.org/d/thi.ng/geom/1.0.1/api/thi.ng.geom.vector#vec3).
68+
69+
Example:
70+
71+
```
72+
(clerk/code "
73+
74+
(let [pos (v/vec2 100 100)
75+
[x y] pos]
76+
;; use x y
77+
)
78+
79+
(let [pos (v/vec3 100 100 100)
80+
[x y z] pos]
81+
;; use x y z
82+
)
83+
")
84+
85+
```
86+
87+
In the following table the namespaces referenced are:
88+
89+
* [`[thi.ng.math.core :as tm]`](https://github.com/thi-ng/math/blob/0.3.2/src/core.org)
90+
* [`[thi.ng.geom.core :as g]`](https://cljdoc.org/d/thi.ng/geom/1.0.1/api/thi.ng.geom.core)
91+
92+
| p5.js Method | `thi.ng` Function | Task |
93+
|------------------|--------------------------|----------------------------------------------------------------------------------|
94+
| `add()` | `(tm/+ a b)` | Adds vector `a` to vector `b` |
95+
| `sub()` | `(tm/- a b` | Subtracts vector `a` from vector `b` |
96+
| `mult()` | `(tm/* a b)` | Scales this vector with multiplication |
97+
| `div()` | `(tm/div a b)` | Scales this vector with division |
98+
| `mag()` | `(tm/mag a)` | Returns the magnitude of the vector `a` |
99+
| `setMag()` | `-` | Sets the magnitude of this vector- Not available AFAICT |
100+
| `normalize()` | `(tm/normalize a)` | Normalizes this vector to a unit length of 1 |
101+
| `limit()` | `(tm/limit a)` | Limits the magnitude of this vector |
102+
| `heading()` | `(g/heading-xy a)` | Returns the 2D heading of this vector expressed as an angle |
103+
| `rotate()` | `(g/rotate a angle)` | Rotates this 2D vector by `angle` (in radians) |
104+
| `lerp()` | `(tm/mix a b amt)` | Linear interpolates vector `a` towards vector `b` by `amt` |
105+
| `dist()` | `(g/dist a b)` | Returns the Euclidean distance between vector `a` and `b` (considered as points) |
106+
| `angleBetween()` | `(g/angle-between a b )` | Finds the angle between vector `a` and vector `b` |
107+
| `dot()` | `(tm/dot a b)` | Returns $$\vec{a} \cdot \vec{b} $$ (the dot product) |
108+
| `cross()` | `(tm/cross a b)` | Returns $$\vec{a} \times \vec{b} $$ (the cross product) |
109+
| `random2D()` | `-` | Returns a random 2D vector - Not available AFAICT |
110+
| `random3D()` | `-` | Returns a random 3D vector - Not available AFAICT |
111+
112+
Some extra notes:
113+
114+
Most of the math functions in `thi.ng` tend to convert integers to doubles. For example `(tm/mag (v/vec2 4 3))` produces `5.0` not `5` (that makes sense for magnitude since it is often a fractional number).
115+
116+
But this it may be surprising. `(tm/+ (v/vec2 1 1) (v/vec2 1 1))` produces a vector with `2.0` as the x and y components, not `2`.
117+
118+
119+
## [Example 1.3: Vector Subtraction](https://natureofcode.com/vectors/#example-13-vector-subtraction)
120+
121+
```clojure
122+
^{::clerk/no-cache true ::clerk/viewer clerk/code}
123+
(slurp "src/noc/chapter_1_3.cljs")
124+
(show-sketch :c1.3)
125+
```
126+
127+
128+
## [Example 1.4: Multiplying a Vector](https://natureofcode.com/vectors/#example-14-multiplying-a-vector)
129+
130+
131+
```clojure
132+
^{::clerk/no-cache true ::clerk/viewer clerk/code}
133+
(slurp "src/noc/chapter_1_4.cljs")
134+
(show-sketch :c1.4)
135+
```
136+
137+
Ok, for this one I got tired of destructuring the vectors or using `first` and `second` to yoink out the components to pass to quil's `line` function. A little helper goes a long way.
138+
139+
By the way, Daniel's exposition on the vector math in this chapter is really great. Don't just look at the code, refer to the text and the illustrations to really get an understanding of the vector math.

src/noc/chapter_1_3.cljs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(ns noc.chapter-1-3
2+
(:require
3+
4+
[thi.ng.math.core :as tm]
5+
[thi.ng.geom.vector :as v]
6+
[quil.core :as q]))
7+
8+
(def size [640 240])
9+
10+
(defn init-state [{:keys [width height] :as state}]
11+
{})
12+
13+
(defn setup! [{:keys [width height]}]
14+
(q/background 255))
15+
16+
(defn tick [state]
17+
state)
18+
19+
(defn draw! [{:keys [width height mouse-x mouse-y]}]
20+
(let [mouse (v/vec2 mouse-x mouse-y)
21+
center (v/vec2 (quot width 2) (quot height 2))
22+
subtracted (tm/- mouse center)]
23+
(q/background 255)
24+
(q/stroke-weight 4)
25+
(q/stroke 200)
26+
(q/line 0 0 mouse-x mouse-y)
27+
(q/line 0 0 (first center) (second center))
28+
(q/stroke 0)
29+
(q/translate (quot width 2) (quot height 2))
30+
(q/line 0 0 (first subtracted) (second subtracted))))

src/noc/chapter_1_4.cljs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns noc.chapter-1-4
2+
(:require
3+
4+
[thi.ng.math.core :as tm]
5+
[thi.ng.geom.vector :as v]
6+
[quil.core :as q]))
7+
8+
(def size [640 240])
9+
10+
(defn init-state [{:keys [width height] :as state}]
11+
{})
12+
13+
(defn setup! [{:keys [width height]}]
14+
(q/background 255))
15+
16+
(defn tick [state]
17+
state)
18+
19+
(defn line [[x1 y1] [x2 y2]]
20+
(q/line x1 y1 x2 y2))
21+
22+
(defn draw! [{:keys [width height mouse-x mouse-y]}]
23+
(let [zero (v/vec2 0 0)
24+
mouse (v/vec2 mouse-x mouse-y)
25+
center (v/vec2 (quot width 2) (quot height 2))
26+
subtracted (tm/- mouse center)
27+
multed (tm/* subtracted 0.5)]
28+
(q/background 255)
29+
(q/translate (quot width 2) (quot height 2))
30+
(q/stroke-weight 2)
31+
(q/stroke 200)
32+
(line zero subtracted)
33+
(q/stroke 0)
34+
(q/stroke-weight 4)
35+
(line zero multed)))

src/noc/sketch.cljs

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
[noc.chapter-0-10e :as c0.10e]
2222
[noc.chapter-1-1 :as c1.1]
2323
[noc.chapter-1-2 :as c1.2]
24-
[noc.chapter-1-3e :as c1.3e]))
24+
[noc.chapter-1-3e :as c1.3e]
25+
[noc.chapter-1-3 :as c1.3]
26+
[noc.chapter-1-4 :as c1.4]))
2527

2628
(def sketches
2729
{:walker (sketch-> c0.1)
@@ -41,7 +43,9 @@
4143
:c0.10e (sketch-3d-> c0.10e)
4244
:c1.1 (sketch-> c1.1)
4345
:c1.2 (sketch-> c1.2)
44-
:c1.3e (sketch-3d-> c1.3e)})
46+
:c1.3e (sketch-3d-> c1.3e)
47+
:c1.3 (sketch-> c1.3)
48+
:c1.4 (sketch-> c1.4)})
4549

4650
(defn load-sketch [s]
4751
(when-let [sk (get sketches s)]

0 commit comments

Comments
 (0)