Skip to content

Instantly share code, notes, and snippets.

@quephird
Created December 12, 2015 00:38
Show Gist options
  • Save quephird/7a7b35c50ae41dc94212 to your computer and use it in GitHub Desktop.
Save quephird/7a7b35c50ae41dc94212 to your computer and use it in GitHub Desktop.
(ns ellipses
(:require [quil.core :as q :include-macros true]
[quil.middleware :as m]))
(defn setup []
(q/background 0)
(q/stroke-weight 0.25)
(q/no-fill)
(q/color-mode :hsb)
(q/no-loop))
(defn foci [k]
(let [a (* (q/cos (/ (* 6.0 q/PI k) 6000.0)) (q/cos (/ (* 16.0 q/PI k) 6000.0)))
b (* (q/sin (/ (* 6.0 q/PI k) 6000.0)) (q/cos (/ (* 16.0 q/PI k) 6000.0)))
c (+ (/ 1.0 200.0) (/ 1.0 14.0) (/ (q/pow (q/sin (/ (* 10.0 q/PI k) 6000.0)) 3.0) 14.0))
x₁ (+ a (* c (q/cos (/ (* 200.0 q/PI k) 6000.0))))
y₁ (+ b (* c (q/sin (/ (* 200.0 q/PI k) 6000.0))))
x₂ (- a (* c (q/cos (/ (* 200.0 q/PI k) 6000.0))))
y₂ (- b (* c (q/sin (/ (* 200.0 q/PI k) 6000.0))))]
[[x₁ y₁] [x₂ y₂]]))
(defn e [k]
(- (/ 49.0 50.0) (/ (q/pow (q/sin (/ (* 6.0 q/PI k) 6000.0)) 2.0) 7.0)))
(defn ellipse' [[[x₁ y₁] [x₂ y₂]] e s]
"Draws an ellipse with foci at (x₁, y₁) and (x₂, y₂)
and eccentricity e and scaled by factor s."
(let [[x₁' y₁' x₂' y₂'] (map #(* s %) [x₁ y₁ x₂ y₂])
dy (- y₂' y₁')
dx (- x₂' x₁')
θ (q/atan2 dy dx)
xₒ (+ x₁' (* 0.5 dx))
yₒ (+ y₁' (* 0.5 dy))
c (* 0.5 (q/sqrt (+ (* dx dx) (* dy dy))))
a (/ c e)
b (q/sqrt (- (* a a) (* c c)))]
(q/push-matrix)
(q/translate xₒ yₒ)
(q/rotate θ)
(q/ellipse 0 0 (* 2.0 a) (* 2.0 b))
(q/pop-matrix)))
(defn draw [state]
(let [w (q/width)
h (q/height)
max-k 6000
[min-e max-e] (reduce (fn [[min-e max-e] k]
(let [curr-e (e k)]
[(min curr-e min-e) (max curr-e max-e)])) [(e 0) (e 0)] (range max-k))]
(q/translate (* 0.5 w) (* 0.5 h))
(doseq [k (range max-k)]
(let [h (q/map-range (e k) min-e max-e 140 180)]
(q/stroke h 255 255)
(ellipse' (foci k) (e k) 400)))
(q/save "omfg.png")))
(q/defsketch ellipses
:title "ellipses"
:size [1440 800]
:setup setup
:draw draw
:middleware [m/fun-mode])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment