Created
December 31, 2020 09:20
-
-
Save shspage/8d2b7b22a223e3e3cc58d76f4ddae2be to your computer and use it in GitHub Desktop.
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
;; grid specification | |
(def interval 40) | |
(def nlines 10) | |
(transform [1 0 0 1 | |
(* nlines interval -0.5) | |
(* nlines interval -0.5)] | |
;; circles as controller | |
(def cs [ | |
(circle [108 31.7872] 114.1578) | |
(circle [279 236.7872] 133.0601) | |
(circle [63 290.7872] 103.0194) | |
]) | |
) | |
;; expansion rate (-1 <= x <= 1) | |
(def expan_rate 1) | |
;;- - - - - - - - - - - - | |
;; circle's center | |
(defn cc [c] (vec2/scale (vec2/+ (nth c 2) (nth c 10)) 0.5)) | |
;; circle's radius | |
(defn cr [c] (/ (vec2/dist (nth c 2) (nth c 10)) 2)) | |
;; magnitude of vector | |
(defn mag [v] (vec2/dist [0 0] v)) | |
;; closest circle(center) from vector p | |
;; returns: [distance, vector from center to p, circle instance] | |
(defn closestCircle [p] | |
(let [ds (for [c cs] | |
(let [v (vec2/- p (cc c))] | |
[(mag v) v c]))] | |
(reduce #(if (< (first %1) (first %2)) %1 %2) (first ds) ds) | |
)) | |
(def range1 (range 0,(* nlines interval), interval)) | |
(def range2 (range 0,(* nlines nlines), nlines)) | |
;; generates points for grid | |
(def points | |
(for [x range1, y range1] | |
(let [o [x y] | |
circle_spec (closestCircle o) | |
d (first circle_spec) | |
v (second circle_spec) | |
r (cr (nth circle_spec 2))] | |
(if (< d r) | |
(vec2/+ o (vec2/scale v | |
(* expan_rate (pow (fit 0 r 1 0 d) 2)))) | |
o) | |
))) | |
:start-sketch | |
(background "#fff") | |
(transform [1 0 0 1 | |
(* nlines interval -0.5) | |
(* nlines interval -0.5)] | |
(style (stroke "black" 2) | |
(let [ps points] | |
(conj | |
(for [x range2] (apply polyline (for [y (range nlines)] (nth ps (+ x y))))) | |
(for [x (range nlines)] (apply polyline (for [y range2] (nth ps (+ x y))))) | |
)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment