-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 1 Examples 9, 10, and Exercises 6, 7, and 8
- Loading branch information
Showing
7 changed files
with
294 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns noc.chapter-1-10 | ||
(:require | ||
[thi.ng.math.core :as tm] | ||
[thi.ng.geom.vector :as v] | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [{:keys [width height] :as state}] | ||
{:mover {:position (v/vec2 (quot width 2) (quot height 2)) | ||
:velocity (v/vec2 0 0) | ||
:top-speed 5}}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
(defn tick-mover [mouse-x mouse-y {:keys [position top-speed velocity] :as mover}] | ||
(let [mouse (v/vec2 mouse-x mouse-y) | ||
direction (tm/- mouse position) | ||
acceleration (tm/* (tm/normalize direction) 0.2) | ||
new-vel (tm/limit (tm/+ velocity acceleration) top-speed)] | ||
(-> mover | ||
(assoc :velocity new-vel) | ||
(update :position #(tm/+ % new-vel))))) | ||
|
||
(defn tick [{:keys [mouse-x mouse-y] :as state}] | ||
(-> state | ||
(update :mover #(tick-mover mouse-x mouse-y %)))) | ||
|
||
(defn draw-mover [{:keys [position]}] | ||
(q/stroke 0) | ||
(q/stroke-weight 2) | ||
(q/fill 127) | ||
(q/ellipse (v/x position) (v/y position) 48 48)) | ||
|
||
(defn draw! [{:keys [mover]}] | ||
(q/background 255) | ||
(draw-mover mover)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(ns noc.chapter-1-6e | ||
(:require | ||
[thi.ng.math.core :as tm] | ||
[thi.ng.geom.vector :as v] | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [{:keys [width height] :as state}] | ||
{:mover {:position (v/vec2 (quot width 2) (quot height 2)) | ||
:velocity (v/vec2 0 0) | ||
:top-speed 5 | ||
:tx 0 | ||
:ty 10000}}) | ||
|
||
(defn setup! [_] | ||
(q/background 255)) | ||
|
||
(defn random-vec2 [tx ty] | ||
(v/vec2 | ||
(q/map-range (q/noise tx) 0 1 -1 1) | ||
(q/map-range (q/noise ty) 0 1 -1 1))) | ||
|
||
(defn enforce-bound [dimension component position] | ||
(let [value (component position)] | ||
(cond (>= value dimension) (assoc position component 0) | ||
(<= value 0) (assoc position component dimension) | ||
:else position))) | ||
|
||
(defn tick-mover [width height {:keys [tx ty top-speed velocity] :as mover}] | ||
(let [acceleration (tm/* (random-vec2 tx ty) 2) | ||
new-vel (tm/limit (tm/+ velocity acceleration) top-speed)] | ||
(-> mover | ||
(assoc :velocity new-vel) | ||
(update :position #(tm/+ % velocity)) | ||
(update :position #(enforce-bound width :x %)) | ||
(update :position #(enforce-bound height :y %)) | ||
(update :tx #(+ % 0.01)) | ||
(update :ty #(+ % 0.01))))) | ||
|
||
(defn tick [{:keys [width height] :as state}] | ||
(-> state | ||
(update :mover #(tick-mover width height %)))) | ||
|
||
(defn draw-mover [{:keys [position]}] | ||
(q/stroke 0) | ||
(q/stroke-weight 2) | ||
(q/fill 127) | ||
(q/ellipse (v/x position) (v/y position) 48 48)) | ||
|
||
(defn draw! [{:keys [mover]}] | ||
(q/background 255) | ||
(draw-mover mover)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns noc.chapter-1-8e | ||
(:require | ||
[thi.ng.geom.rect :as r] | ||
[thi.ng.math.core :as tm] | ||
[thi.ng.geom.core :as g] | ||
[thi.ng.geom.vector :as v] | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [{:keys [width height] :as state}] | ||
{:mover {:position (v/vec2 (quot width 2) (quot height 2)) | ||
:velocity (v/vec2 0 0) | ||
:top-speed 5}}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
(defn tick-mover [canvas-rect mouse {:keys [position top-speed velocity] :as mover}] | ||
(if (g/contains-point? canvas-rect mouse) | ||
(let [direction (tm/- mouse position) | ||
dist (g/dist position mouse) | ||
acceleration (tm/* (tm/normalize direction) (/ 2 dist)) | ||
new-vel (tm/limit (tm/+ velocity acceleration) top-speed)] | ||
(-> mover | ||
(assoc :velocity new-vel) | ||
(update :position #(tm/+ % new-vel)))) | ||
(-> mover | ||
(assoc :velocity (v/vec2 0 0))))) | ||
|
||
(defn tick [{:keys [width height mouse-x mouse-y] :as state}] | ||
(-> state | ||
(update :mover #(tick-mover (r/rect 0 0 width height) (v/vec2 mouse-x mouse-y) %)))) | ||
|
||
(defn draw-mover [{:keys [position]}] | ||
(q/stroke 0) | ||
(q/stroke-weight 2) | ||
(q/fill 127) | ||
(q/ellipse (v/x position) (v/y position) 48 48)) | ||
|
||
(defn draw! [{:keys [mover]}] | ||
(q/background 255) | ||
(draw-mover mover)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
(ns noc.chapter-1-9 | ||
(:require | ||
[thi.ng.math.core :as tm] | ||
[thi.ng.geom.vector :as v] | ||
[quil.core :as q])) | ||
|
||
(def size [640 240]) | ||
|
||
(defn init-state [{:keys [width height] :as state}] | ||
{:mover {:position (v/vec2 (quot width 2) (quot height 2)) | ||
:velocity (v/vec2 0 0) | ||
:top-speed 5}}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
(defn random-vec2 [] | ||
(let [[x y] (q/random-2d)] | ||
(v/vec2 x y))) | ||
|
||
(defn enforce-bound [dimension component position] | ||
(let [value (component position)] | ||
(cond (>= value dimension) (assoc position component 0) | ||
(<= value 0) (assoc position component dimension) | ||
:else position))) | ||
|
||
(defn tick-mover [width height {:keys [top-speed velocity] :as mover}] | ||
(let [acceleration (tm/* (random-vec2) 2) | ||
new-vel (tm/limit (tm/+ velocity acceleration) top-speed)] | ||
(-> mover | ||
(assoc :velocity new-vel) | ||
(update :position #(tm/+ % velocity)) | ||
(update :position #(enforce-bound width :x %)) | ||
(update :position #(enforce-bound height :y %))))) | ||
|
||
(defn tick [{:keys [width height] :as state}] | ||
(-> state | ||
(update :mover #(tick-mover width height %)))) | ||
|
||
(defn draw-mover [{:keys [position]}] | ||
(q/stroke 0) | ||
(q/stroke-weight 2) | ||
(q/fill 127) | ||
(q/ellipse (v/x position) (v/y position) 48 48)) | ||
|
||
(defn draw! [{:keys [mover]}] | ||
(q/background 255) | ||
(draw-mover mover)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters