1439d

(ns seni.sketch-1439d
  (:require #+clj  [seni.render :as r]
            #+cljs [seni.render :as r :include-macros true]
            [seni.math :as m]
            [seni.perlin :as p]
            [seni.movement :as mv]
            [seni.bezier :as b]
            [seni.colour :as c]
            [seni.toolbox :as t]))

(defn bezier-stroked-coloured-rect
  [& {:keys [x y width height col col-volatility volatility overlap iterations seed]}]
  (let [th-width (/ width 3)
        th-height (/ height 3)
        vol volatility

        h-delta (/ height iterations)
        h-strip-width (/ height iterations)
        half-h-strip-width (/ h-strip-width 2)

        v-delta (/ width iterations)
        v-strip-width (/ width iterations)
        half-v-strip-width (/ v-strip-width 2)

        half-alpha-col (assoc col :alpha (/ (:alpha col) 2))
        lab (c/convert half-alpha-col :lab)]

    ; horizontal strips
    (loop [i iterations
           prngs (m/lazy-seq-prng-0 seed)]
      (when (> i 0)
        (let [[rx1 ry1 rx2 ry2 rx3 ry3 rx4 ry4 rcol] (take 9 prngs)]
          (r/fill-colour (assoc lab :l (+ (:l lab) (* col-volatility rcol))))
          (b/render-bezier 10 (+ overlap h-strip-width)
                           (+ (+ (* rx1 vol) x) (* 0 th-width))
                           (- (+ (* i h-delta) (* ry1 vol) y) half-h-strip-width)

                           (+ (+ (* rx2 vol) x) (* 1 th-width))
                           (- (+ (* i h-delta) (* ry2 vol) y) half-h-strip-width)

                           (+ (+ (* rx3 vol) x) (* 2 th-width))
                           (- (+ (* i h-delta) (* ry3 vol) y) half-h-strip-width)

                           (+ (+ (* rx4 vol) x) (* 3 th-width))
                           (- (+ (* i h-delta) (* ry4 vol) y) half-h-strip-width))
          (recur (dec i) (drop 9 prngs)))))

    ; vertical strips
    (loop [i iterations
           prngs (m/lazy-seq-prng-0 (* seed seed))]
      (when (> i 0)
        (let [[rx1 ry1 rx2 ry2 rx3 ry3 rx4 ry4 rcol] (take 9 prngs)]
          (r/fill-colour (assoc lab :l (+ (:l lab) (* col-volatility rcol))))
          (b/render-bezier 10 (+ overlap v-strip-width)
                           (- (+ (* i v-delta) (* rx1 vol) x) half-v-strip-width)
                           (+ (+ (* ry1 vol) y) (* 0 th-height))

                           (- (+ (* i v-delta) (* rx2 vol) x) half-v-strip-width)
                           (+ (+ (* ry2 vol) y) (* 1 th-height))

                           (- (+ (* i v-delta) (* rx3 vol) x) half-v-strip-width)
                           (+ (+ (* ry3 vol) y) (* 2 th-height))

                           (- (+ (* i v-delta) (* rx4 vol) x) half-v-strip-width)
                           (+ (+ (* ry4 vol) y) (* 3 th-height)))
          (recur (dec i) (drop 9 prngs)))))))

(defn draw [state]
  (r/background-float 255)
  (t/wash 160 160 160 100)

  (let [n 10]
   (doseq [xp (range 1 (- n 1))
           yp (range 1 (- n 1))]
     (let [w (/ 1000 n)
           h (/ 1000 n)]
       (bezier-stroked-coloured-rect :x (+ (* w xp) 20) :y (+ (* h yp) 20)
                                     :width (- w 40) :height (- h 40)
                                     :col-volatility 8
                                     :col (c/rgb :r 0.8 :g 0.0 :b 0.0 :alpha 1.0)
                                     :volatility (* 3 (m/distance-2d xp yp 5 5))
                                     :iterations 8
                                     :seed (+ (* xp n) yp)
                                     :overlap 3.0)))))

(r/defsketch sketch-1439d
  :title "squares"
  :aspect :square
  :canvas-width 600
  :draw draw)