From f61bc8c68513e95f3ed3b56a1074597e7c92af7c Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 26 Feb 2022 09:37:40 -0500 Subject: [PATCH] [WIP] Finish branches implementation, test draw function --- cljs/jeremy_website/sketches/tree.cljs | 84 +++++++++++++++----------- 1 file changed, 50 insertions(+), 34 deletions(-) diff --git a/cljs/jeremy_website/sketches/tree.cljs b/cljs/jeremy_website/sketches/tree.cljs index fbd253d..ffd0e20 100644 --- a/cljs/jeremy_website/sketches/tree.cljs +++ b/cljs/jeremy_website/sketches/tree.cljs @@ -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))