diff --git a/src/shimmers/sketches/patterns_shifted.cljs b/src/shimmers/sketches/patterns_shifted.cljs index 2969382cd..fab3dd78b 100644 --- a/src/shimmers/sketches/patterns_shifted.cljs +++ b/src/shimmers/sketches/patterns_shifted.cljs @@ -3,9 +3,10 @@ [clojure.set :as set] [shimmers.common.svg :as csvg :include-macros true] [shimmers.common.ui.controls :as ctrl] + [shimmers.common.ui.debug :as debug] [shimmers.math.deterministic-random :as dr] [shimmers.math.geometry.collisions :as collide] - [shimmers.math.geometry.triangle :as triangle] + [shimmers.math.geometry.polygon :as poly] [shimmers.sketch :as sketch :include-macros true] [shimmers.view.sketch :as view-sketch] [thi.ng.geom.circle :as gc] @@ -15,6 +16,7 @@ [thi.ng.geom.vector :as gv] [thi.ng.math.core :as tm])) +(defonce defo (debug/state [])) (def width 800) (def height 600) (def bounds (rect/rect 0 0 width height)) @@ -22,9 +24,9 @@ (gv/vec2 (* width x) (* height y))) (defn random-shape [size] - (dr/weighted - {(triangle/inscribed-equilateral (gv/vec2) size 0) 2 - (rect/rect size) 0})) + (let [n (dr/weighted {3 8 4 4 5 1 6 2 7 1 8 1}) + r (poly/circumradius-side-length n size)] + (poly/regular-n-gon n r))) (defn gen-shapes [size shape n] (loop [structure [shape] @@ -37,27 +39,33 @@ mid (tm/mix fp fq 0.5) structure-face (g/normal (tm/- fq fp)) shape (g/rotate (random-shape size) (g/heading (tm/- structure-face))) - pos (tm/- mid (tm/* structure-face 0.4)) - shape' (g/translate shape pos) + apothem (poly/apothem-side-length (count (g/vertices shape)) size) + pos (tm/- mid (tm/normalize structure-face (+ apothem 0.01))) + shape' (g/center shape pos) edges (drop 1 (sort-by (fn [[p q]] (g/dist-squared mid (tm/mix p q 0.5))) - (g/edges shape')))] - (if (or (not (collide/bounded? bounds shape')) - (some (fn [s] (collide/overlaps? s shape')) structure)) - (recur structure - (disj faces face) - annotation) + (g/edges shape'))) + inside? (collide/bounded? bounds shape') + overlaps? (some (fn [s] (collide/overlaps? s shape')) structure)] + (swap! defo conj + {:shape shape + ;; :faces faces + :face face + :added [inside? overlaps?]}) + (if (and inside? (not overlaps?)) (recur (conj structure shape') (set/union (disj faces face) (set edges)) - (conj annotation (gc/circle mid 2.0)))))))) + (into annotation [(gc/circle mid 2.0)])) + (recur structure + (disj faces face) + annotation)))))) (defn shapes [] - (let [starting (vary-meta (g/center (random-shape (* 0.05 height)) + (let [size (* 0.08 height) + starting (vary-meta (g/center (random-shape size) (rv 0.5 0.5)) - assoc :stroke-width 2.0) + assoc :stroke "#772222") {:keys [structure annotation faces]} - (gen-shapes (* 0.05 height) - starting - 32)] + (gen-shapes size starting 32)] (conj (concat structure annotation) (csvg/group {:stroke-width 2.0} (mapv (fn [[p q]] (gl/line2 p q)) faces))))) @@ -68,6 +76,7 @@ :stroke "black" :fill "none" :stroke-width 0.5} + (reset! defo []) (shapes))) (defn page [] @@ -75,7 +84,8 @@ [sketch/with-explanation [:div.canvas-frame [scene]] [view-sketch/generate :patterns-shifted] - [:div.readable-width]])) + [:div.readable-width + [debug/display defo]]])) (sketch/definition patterns-shifted {:created-at "2024-05-23"