Skip to content

Commit

Permalink
switch to expanding with random regular shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed May 25, 2024
1 parent 28447ac commit 2a564d5
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/shimmers/sketches/patterns_shifted.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -15,16 +16,17 @@
[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))
(defn rv [x y]
(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]
Expand All @@ -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)))))
Expand All @@ -68,14 +76,16 @@
:stroke "black"
:fill "none"
:stroke-width 0.5}
(reset! defo [])
(shapes)))

(defn page []
(fn []
[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"
Expand Down

0 comments on commit 2a564d5

Please sign in to comment.