[WIP] Finish branches implementation, test draw function

This commit is contained in:
Jeremy Dormitzer 2022-02-26 09:37:40 -05:00
parent 59876ea2d8
commit f61bc8c685

View File

@ -17,7 +17,7 @@
(fn make-tree [segment-length x y angle symbols branch idx]
(let [[symbol & rest-symbols] symbols]
(condp = symbol
nil branch
nil [branch rest-symbols]
"F" (let [new-x (+ x (* segment-length
(js/Math.cos (degrees->radians angle))))
new-y (+ y (* (- segment-length)
@ -25,8 +25,10 @@
branch (assoc branch
:segments
(conj (:segments branch)
{:length segment-length
:angle angle}))]
{:x1 x
:y1 y
:x2 new-x
:y2 new-y}))]
(recur segment-length new-x new-y angle rest-symbols branch (inc idx)))
"+" (recur segment-length x y (+ angle 25) rest-symbols branch idx)
"-" (recur segment-length x y (- angle 25) rest-symbols branch idx)
@ -34,30 +36,30 @@
"/" (recur segment-length x y (- angle 20) rest-symbols branch idx)
">" (recur segment-length x y (+ angle 15) rest-symbols branch idx)
"<" (recur segment-length x y (- angle 15) rest-symbols branch idx)
"[" (let [new-branch (make-tree segment-length
x
y
angle
rest-symbols
{:x x
:y y
:segments []
:children {}}
0)
"[" (let [[new-branch rest-symbols] (make-tree segment-length
x
y
angle
rest-symbols
{:x x
:y y
:segments []
:children {}}
0)
branch (assoc-in branch [:children idx] new-branch)]
(recur segment-length x y angle rest-symbols branch idx))
"]" branch ;; stop processing current branch and pop to parent call
"]" [branch rest-symbols] ;; stop processing current branch and pop to parent call
(recur segment-length x y angle rest-symbols branch idx))))]
(make-tree segment-length
initial-x
initial-y
initial-angle
(:state l-system)
{:x initial-x
:y initial-y
:segments []
:children {}}
0)))
(first (make-tree segment-length
initial-x
initial-y
initial-angle
(:state l-system)
{:x initial-x
:y initial-y
:segments []
:children {}}
0))))
(defn l-system-lines
[{:keys [segment-length initial-angle initial-x initial-y l-system]}]
@ -123,15 +125,18 @@
:initial-x (* (q/width) 0.1)
:initial-y (* (q/height) 0.95)
:l-system l-system})
lines (l-system-lines {:initial-angle (- 90 25)
:segment-length 6
:initial-x (* (q/width) 0.1)
:initial-y (* (q/height) 0.95)
:l-system l-system})]
{:to-draw lines
:drawing []}))
;; lines (l-system-lines {:initial-angle (- 90 25)
;; :segment-length 6
;; :initial-x (* (q/width) 0.1)
;; :initial-y (* (q/height) 0.95)
;; :l-system l-system})
]
{:tree tree
;; :to-draw lines
;; :drawing []
}))
(defn update-state [{:keys [to-draw drawing] :as state}]
(defn old-update-state [{:keys [to-draw drawing] :as state}]
(let [batch-size 10]
(if (> (count to-draw) 0)
(assoc state
@ -151,7 +156,7 @@
:already-seen #{}}
lines)))
(defn draw [{:keys [to-draw drawing]}]
(defn old-draw [{:keys [to-draw drawing]}]
(q/background 0 0 100)
(q/fill 0 0 0)
(let [lines (dedupe-lines drawing)]
@ -162,6 +167,17 @@
(when (= (count to-draw) 0)
(q/no-loop)))
(defn draw [{:keys [tree]}]
(q/background 0 0 100)
(let [draw-tree
(fn draw-tree [branch]
(doseq [segment (:segments branch)]
(q/line (:x1 segment) (:y1 segment) (:x2 segment) (:y2 segment)))
(doseq [child (vals (:children branch))]
(draw-tree child)))]
(draw-tree tree)
(q/no-loop)))
(defn sketch
[{:keys [host size]}]
(q/sketch
@ -169,5 +185,5 @@
:middleware [m/fun-mode]
:size size
:setup setup
:update update-state
;; :update update-state
:draw draw))