-
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.
- Loading branch information
Showing
7 changed files
with
180 additions
and
3 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,29 @@ | ||
(ns noc.chapter-1-5 | ||
(:require | ||
[noc.quil.util :as u] | ||
[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}] | ||
{}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
(defn tick [state] | ||
state) | ||
|
||
(defn draw! [{:keys [width height mouse-x mouse-y]}] | ||
(let [zero (v/vec2 0 0) | ||
mouse (v/vec2 mouse-x mouse-y) | ||
center (v/vec2 (quot width 2) (quot height 2)) | ||
subtracted (tm/- mouse center) | ||
m (tm/mag subtracted)] | ||
(q/background 255) | ||
(q/fill 0) | ||
(q/rect 10 10 m 10) | ||
(q/translate (quot width 2) (quot height 2)) | ||
(u/line zero subtracted))) |
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,32 @@ | ||
(ns noc.chapter-1-6 | ||
(:require | ||
[noc.quil.util :as u] | ||
[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}] | ||
{}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
(defn tick [state] | ||
state) | ||
|
||
(defn draw! [{:keys [width height mouse-x mouse-y]}] | ||
(let [zero (v/vec2 0 0) | ||
mouse (v/vec2 mouse-x mouse-y) | ||
center (v/vec2 (quot width 2) (quot height 2)) | ||
subtracted (tm/- mouse center) | ||
normed (tm/* (tm/normalize subtracted) 50)] | ||
(q/background 255) | ||
(q/translate (quot width 2) (quot height 2)) | ||
(q/stroke 200) | ||
(q/stroke-weight 2) | ||
(u/line zero subtracted) | ||
(q/stroke 0) | ||
(q/stroke-weight 8) | ||
(u/line zero normed))) |
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,59 @@ | ||
(ns noc.chapter-1-7 | ||
(:require | ||
[noc.quil.util :as u] | ||
[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 (q/random width) (q/random height)) | ||
:velocity (v/vec2 (q/random -2 2) (q/random -2 2))}}) | ||
|
||
(defn setup! [{:keys [width height]}] | ||
(q/background 255)) | ||
|
||
;; These were my first pass at the implementation | ||
;; but I wasn't happy that it has so much repeated code | ||
(comment | ||
(defn enforce-bound-x [width position] | ||
(let [x (v/x position)] | ||
(cond (>= x width) (v/vec2 0 (v/y position)) | ||
(<= x 0) (v/vec2 width (v/y position)) | ||
:else position))) | ||
|
||
(defn enforce-bound-y [height position] | ||
(let [y (v/y position)] | ||
(cond (>= y height) (v/vec2 (v/y position) 0) | ||
(<= y 0) (v/vec2 (v/y position) height) | ||
:else position)))) | ||
|
||
;; ... so I refactored it to this | ||
;; `dimension` is either width or height | ||
;; `component` is either :x or :y, perhaps there's a better name? | ||
(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 [velocity] :as mover}] | ||
(-> mover | ||
(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(ns noc.quil.util | ||
(:require [quil.core :as q])) | ||
|
||
(defn line [[x1 y1] [x2 y2]] | ||
(q/line x1 y1 x2 y2)) |
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