Skip to content

Instantly share code, notes, and snippets.

@quephird
Last active December 14, 2015 15:39
Show Gist options
  • Save quephird/5109693 to your computer and use it in GitHub Desktop.
Save quephird/5109693 to your computer and use it in GitHub Desktop.
Somewhat rough implementation to generate Ford circles, http://en.wikipedia.org/wiki/Farey_sequence#Ford_circles.
(ns ford-circles
(:use quil.core quil.applet))
(def screen-w 1920)
(def screen-h 1080)
; This would likely be deemed a hack by a seasoned mathematician;
; I am sure there is a more efficient incantation to produce these numbers.
(defn farey-seq [n]
(apply sorted-set
(filter #(< % 1)
(let [ns (range 1 (inc n))]
(for [p ns q ns] (/ p q))))))
(defn setup []
(background 0)
(smooth)
(no-stroke)
(no-loop)
)
; Circles correspondent with the same q value will be drawn with the same color;
; this makes the resultant drawing symmetric.
(defn draw []
(let [[r b] [(random 256) (random 256)]]
(doseq [n (farey-seq 20)]
(let [p (numerator n)
q (denominator n)
d (/ screen-w (* q q))
c (map #(mod % 255) [(* r q) 0 (* b q)])
x (* screen-w n)
y (- (* screen-h 0.67) (* 0.5 d))]
(apply fill c)
(ellipse x y d d))
)
)
(save "ford-circles.png")
)
(defsketch main
:title "ford circles"
:setup setup
:draw draw
:size [screen-w screen-h]
:renderer :opengl
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment