155j Chaotic Grid 3

(meta linear-colour-space: 1)

(render-background)
(translate vector: [(/ canvas/width 2) (/ canvas/height 2)])
(rotate angle: {5 (gen/scalar max: 115)})
(scale scalar: {1.2 (gen/scalar min: 0.1 max: 3.0)})
(render-main)

(define
  num-squares-to-render {15 (gen/int min: 2 max: 20)}
  gap-size {30 (gen/int min: 5 max: 50)}
  num-squares (+ 2 num-squares-to-render)
  num-gaps (+ num-squares 1)
  square-size (/ (- canvas/width (* gap-size num-gaps)) num-squares)
  base-colour (col/rgb r: {1.0 (gen/scalar)}
                       g: {0.0 (gen/scalar)}
                       b: {0.3 (gen/scalar)}
                       alpha: 1.0)
  colour-volatility {20 (gen/int min: 2 max: 40)}
  focal-point (focal/build-point position: [{500 (gen/int max: canvas/width)}
                                      {500 (gen/int max: canvas/height)}]
                           distance: {500 (gen/int max: canvas/width)})
  focal-power {50 (gen/scalar max: 200)})

(fn (render-background colour: (col/rgb r: 1.0 g: 1.0 b: 0.9))
    (rect position: [(/ canvas/width 2) (/ canvas/height 2)]
          width: canvas/width
          height: canvas/height
          colour: colour))

(fn (map-to-position at: 0)
  (- (+ (* (+ gap-size square-size) at) (/ square-size 2) gap-size)
     (/ canvas/width 2)))

(fn (render-main)
  (loop (y from: 1 to:  (- num-squares 1))
    (loop (x from: 1 to:  (- num-squares 1))
      (define
        p [(map-to-position at: x) (map-to-position at: y)]
        volatility (* (focal/value from: focal-point position: p) focal-power))
      (stroked-bezier-rect position: p
                           width: square-size
                           height: square-size
                           seed: (+ x (* y num-squares))
                           colour: base-colour
                           colour-volatility: colour-volatility
                           volatility: volatility
                           iterations: {10 (gen/int min: 1 max: 50)}
                           tessellation: 1
                           overlap: {0 (gen/int max: 10)}
                           stroke-noise: 0))))


(fn (stroked-bezier-rect position: [100 100]
                         width: 80
                         height: 600
                         volatility: 30
                         overlap: 0
                         iterations: 10
                         seed: 0

                         tessellation: 15
                         stroke-tessellation: 10
                         stroke-noise: 25

                         colour: (col/rgb r: 0.0 g: 1.0 b: 0.0 alpha: 0.5)
                         colour-volatility: 0

                         brush: brush/flat
                         brush-subtype: 1)
  (define
    [x y] position

    x-start (- x (/ width 2.0))
    y-start (- y (/ height 2.0))

    th-width (/ width 3.0)
    th-height (/ height 3.0)

    vol volatility

    cc-lab(colour.col/convert format: LAB)
    half-alpha-col (col/set-alpha from: cc-lab value: (* 0.5 (cc-lab.col/alpha)))

    prng (prng/build seed min: -1 max: 1)

    sum-thickness (+ height (* (- iterations 1.0) overlap))
    stroke-thickness (/ sum-thickness iterations)
    stroke-half-thickness (/ stroke-thickness 2.0)
    stroke-offset-factor (/ (- height overlap) iterations)

    v-sum-thickness (+ width (* (- iterations 1.0) overlap))
    v-stroke-thickness (/ v-sum-thickness iterations)
    v-stroke-half-thickness (/ v-stroke-thickness 2.0)
    v-stroke-offset-factor (/ (- width overlap) iterations))

  ;; horizontal strokes
  (loop (i to: iterations)
    (define
      h (+ y-start stroke-half-thickness (* i stroke-offset-factor))
      coords [[(+ (* (prng.prng/value) vol) x-start (* 0 th-width))
               (+ h (* (prng.prng/value) vol))]
              [(+ (* (prng.prng/value) vol) x-start (* 1 th-width))
               (+ h (* (prng.prng/value) vol))]
              [(+ (* (prng.prng/value) vol) x-start (* 2 th-width))
               (+ h (* (prng.prng/value) vol))]
              [(+ (* (prng.prng/value) vol) x-start (* 3 th-width))
               (+ h (* (prng.prng/value) vol))]])
    (stroked-bezier tessellation
                    coords
                    stroke-tessellation
                    stroke-noise
                    stroke-line-width-start: stroke-thickness
                    stroke-line-width-end: stroke-thickness
                    colour: half-alpha-col
                    colour-volatility
                    seed: (prng.prng/value)
                    brush
                    brush-subtype))

  ;; vertical strokes
  (loop (i to: iterations)
    (define
      v (+ x-start v-stroke-half-thickness (* i v-stroke-offset-factor))
      coords [[(+ v (* (prng.prng/value) vol))
               (+ (* (prng.prng/value) vol) y-start (* 0 th-height))]
              [(+ v (* (prng.prng/value) vol))
               (+ (* (prng.prng/value) vol) y-start (* 1 th-height))]
              [(+ v (* (prng.prng/value) vol))
               (+ (* (prng.prng/value) vol) y-start (* 2 th-height))]
              [(+ v (* (prng.prng/value) vol))
               (+ (* (prng.prng/value) vol) y-start (* 3 th-height))]])
    (stroked-bezier tessellation
                    coords
                    stroke-tessellation
                    stroke-noise
                    stroke-line-width-start: stroke-thickness
                    stroke-line-width-end: stroke-thickness
                    colour: half-alpha-col
                    colour-volatility
                    seed: (prng.prng/value)
                    brush
                    brush-subtype)))