1439

(ns seni.sketch-1439
  (: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 accumulated-rect
  [& {:keys [x y width height col volatility]}]

  (let [half-width (/ width 2)
        half-height (/ height 2)
        passes 10
        prngs (m/lazy-seq-prng-0 0.2)
        [cr cg cb ca] (c/map-to-byte-range col)]

    (r/push-matrix)
    ; translate to the centre of the rectangle
    (r/translate (+ x half-width) (+ y half-height))

    (doseq [[rr xr yr] (partition 3 (take (* 3 passes) prngs))]
      (r/push-matrix)

      (r/rotate (* rr 0.02 volatility))
      (r/translate (* xr 5 volatility) (* yr 5 volatility))

      (r/fill-float cr cg cb (/ ca passes))
      (r/rect (- half-width) (- half-height) width height)

      (r/pop-matrix))
    (r/pop-matrix)))


(defn bezier-stroked-rect
  [& {:keys [x y width height col volatility overlap]}]

  (let [th-width (/ width 3)
        th-height (/ height 3)
        vol volatility
        iterations 10

        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)

        [cr cg cb ca] (c/map-to-byte-range col)
        half-alpha (/ ca 2)]


;    (r/fill-float 0 0 200 100)
;    (r/rect x y width height)


    ; horizontal strips

    (loop [i iterations
           prngs (m/lazy-seq-prng-0 0.2)]
      (when (> i 0)
        (let [[rx1 ry1 rx2 ry2 rx3 ry3 rx4 ry4] (take 8 prngs)]
          (r/fill-float cr cg cb half-alpha)
          (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 8 prngs)))))



    ; vertical strips
    (loop [i iterations
           prngs (m/lazy-seq-prng-0 0.4)]
      (when (> i 0)
        (let [[rx1 ry1 rx2 ry2 rx3 ry3 rx4 ry4] (take 8 prngs)]
          (r/fill-float cr cg cb half-alpha)
          (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 8 prngs)))))))


(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)
        ]

;    (r/fill-float 0 0 200 100)
;    (r/rect x y width height)

    ; 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)


                                        ; rectangle rendered with rect
  (r/fill-float 200 0 0 200)
  (r/rect 20 20 460 460)

                                        ; rectangle rendered with rect
  (accumulated-rect :x 520 :y 20
                    :width 460 :height 460
                    :col (c/rgb :r 0.8 :g 0.0 :b 0.0 :alpha 1.0)
                    :volatility 1.5)

  ; rectangle rendered with bezier curves
  (bezier-stroked-rect :x 20 :y 520
                       :width 460 :height 460
                       :col (c/rgb :r 0.8 :g 0.0 :b 0.0 :alpha 1.0)
                       :volatility 10.0
                       :overlap 3.0)


  (bezier-stroked-coloured-rect :x 520 :y 520
                                :width 460 :height 460
                                :col (c/rgb :r 0.8 :g 0.0 :b 0.0 :alpha 1.0)
                                :col-volatility 5
                                :volatility 10.0
                                :overlap 3.0
                                :iterations 79
                                :seed 0.3))

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