You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notebooks/chapter_1.md
+81
Original file line number
Diff line number
Diff line change
@@ -56,3 +56,84 @@ I am skipping these exercises as they don't add much to what we did in Example 1
56
56
```
57
57
58
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.
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:
|`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`.
## [Example 1.4: Multiplying a Vector](https://natureofcode.com/vectors/#example-14-multiplying-a-vector)
129
+
130
+
131
+
```clojure
132
+
^{::clerk/no-cachetrue::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.
0 commit comments