Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Last active January 28, 2023 18:39
Show Gist options
  • Save jackrusher/6722751 to your computer and use it in GitHub Desktop.
Save jackrusher/6722751 to your computer and use it in GitHub Desktop.
The superformula in clojure: http://en.wikipedia.org/wiki/Superformula
(defn setup []
(smooth)
(no-fill))
(defn superformula-point [m n1 n2 n3 phi]
(let [t1 (pow (abs (cos (/ (* m phi) 4.0))) n2)
t2 (pow (abs (sin (/ (* m phi) 4.0))) n3)
r (pow (+ t1 t2) (/ 1 n1))]
(if (= 0 (abs r))
[0 0]
(let [r (/ 1 r)
x (* r (cos phi))
y (* r (sin phi))]
[x y]))))
(defn superformula [m n1 n2 n3]
(let [phi (/ TWO-PI 360)]
(mapv #(superformula-point m n1 n2 n3 (* phi %))
(range 0 361))))
;; the first four shapes
(def super-shapes [[3 5 18 18]
[6 20 7 18]
[4 2 4 13]
[7 3 4 17]])
(defn draw []
(frame-rate 1)
(background 0)
(no-fill)
(stroke-weight 2)
(stroke 0xff 0x99 0x66 0x96)
(with-translation [(/ (width) 2) (/ (height) 2)]
(with-rotation [(- (* 0.3 (frame-count)))]
(let [scaler 80 ; some shapes are too big or small
points (apply superformula (nth super-shapes 3))] ;; change this for other shapes
(begin-shape)
(doseq [p points]
(apply curve-vertex (map (partial * scaler) p)))
(end-shape)))))
(defsketch superformula-sketch
:title "Form+Code Superformula"
:setup setup
:draw draw
:size [500 500])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment