Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns de-jong.core
- (:require [quil.core :as q]
- [quil.middleware :as m]))
- (set! *warn-on-reflection* true)
- (set! *unchecked-math* :warn-on-boxed)
- (def ^:const ^int width 500)
- (def ^:const ^int height 500)
- (def ^:const ^float l (float (* 0.12 (min width height))))
- (def ^:const ^float h (float (* 0.88 (min width height))))
- (def ^:const ^int steps-per-frame 10000)
- (defn make-attractor
- "Generates an de Jong attractor around the given parameters"
- [^double a ^double b ^double c ^double d]
- (fn attractor
- [[^double x ^double y]]
- [(- ^float (q/sin (* a y)) ^float (q/cos (* b x)))
- (- ^float (q/sin (* c x)) ^float (q/cos (* d y)))]))
- (defn setup []
- (q/frame-rate 30)
- (q/color-mode :hsb)
- (q/background 20)
- (q/fill 20)
- (let [a -1.68661
- b -1.99168
- c 1.71743
- d -1.64958]
- {:attractor (iterate (make-attractor a b c d) [0 0])
- :histogram (int-array (* width height) 0)})) ; amount of times each pixel has been 'visited'
- (defn ->screen-space
- "Takes a coordinate in de Jong space [-2, 2]x[-2, 2] and maps them to a range
- from l to h."
- [coord l h]
- (map #(Math/round ^float (q/map-range % -2 2 l h)) coord))
- (defn update-state [{:keys [attractor histogram] :as state}]
- (let [coords (take steps-per-frame attractor)]
- {:attractor (drop steps-per-frame attractor)
- :histogram (loop [^ints histogram histogram
- coords (map #(->screen-space % l h) coords)]
- (if (empty? coords)
- histogram
- (let [[^int x ^int y] (first coords)]
- (recur
- (let [idx (+ (* y width) x)
- v (inc (aget histogram idx))]
- (aset-int histogram idx v)
- histogram)
- (rest coords)))))}))
- (defn draw-state [{:keys [^ints histogram]}]
- ;; clear drawing
- (q/no-stroke)
- (q/rect 0 0 (q/width) (q/height))
- ;; draw all dots with alpha values from 0 to 255
- (dotimes [y width]
- (let [row (* y width)]
- (dotimes [x height]
- (let [idx (+ row x)]
- (let [v (aget histogram idx)]
- (when-not (zero? v)
- (q/stroke 122 255 255 (* 50.0 ^float (q/log (inc v))))
- (q/point x y))))))))
- (q/defsketch de-jong
- :title "De-Jong"
- :size [width height]
- :setup setup
- :update update-state
- :draw draw-state
- :features [:keep-on-top]
- :middleware [m/pause-on-error m/fun-mode])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement