Skip to content

Instantly share code, notes, and snippets.

@voxlet
Last active October 2, 2017 07:19
Show Gist options
  • Select an option

  • Save voxlet/573e5c0e723ac24dcd8f03ce7799fa5d to your computer and use it in GitHub Desktop.

Select an option

Save voxlet/573e5c0e723ac24dcd8f03ce7799fa5d to your computer and use it in GitHub Desktop.
(ns quil-test.draw
(:require [quil.core :as quil]
[clojure.core.matrix :as matrix]
[clojure.core.matrix.operators :refer [+ - *]])
(:refer-clojure :exclude [+ - *])
(:import [java.util Date]))
(def canvas-h 800)
(def canvas-w (* canvas-h 1.618))
(defn offset-midpoint [[a b]]
(let [[xa ya] a
[xb yb] b
n (matrix/normalise [(- ya yb) (- xb xa)])
mid (* (+ b a) 0.5)
offset (-> (- b a)
(matrix/length)
(* 0.33 (- (quil/random-gaussian) 0.2)))]
(+ mid (* n offset))))
(defn bend [vs]
(let [bent (->> (partition 2 1 vs)
(pmap offset-midpoint)
(interleave vs)
(into []))]
(conj bent (last vs))))
(defn spread [curves]
(let [step (/ (float canvas-h) (- (count curves) 3))]
(map-indexed
(fn [i vs]
(mapv (fn [[x y]] [x (+ y (- canvas-h (* step (dec i))))]) vs))
curves)))
(defn add-ends [vs]
(let [f [0 canvas-h]
l [canvas-w canvas-h]
offs [100 0]]
(-> (into [f f (- (first vs) offs)] vs)
(into [(+ (last vs) offs) l l]))))
(defn curve [vs]
(quil/begin-shape)
(mapv #(apply quil/curve-vertex %) vs)
(quil/end-shape))
(defn setup []
(quil/frame-rate 0.5)
(quil/color-mode :hsb 360 100 100 100))
(defn draw []
(quil/background 0 0 100 100)
(quil/stroke 0 0 0 20)
(quil/fill 0 0 0 5)
(->> (iterate bend [[0 0] [canvas-w 0]])
(take 18)
(spread)
(map add-ends)
(mapv curve))
(quil/save (str (.toInstant (Date.)) ".png")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment