Advertisement
Guest User

de-jong optimized

a guest
Oct 8th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns de-jong.core
  2.   (:require [quil.core :as q]
  3.             [quil.middleware :as m]))
  4.  
  5. (set! *warn-on-reflection* true)
  6. (set! *unchecked-math* :warn-on-boxed)
  7.  
  8. (def ^:const ^int width 500)
  9. (def ^:const ^int height 500)
  10.  
  11. (def ^:const ^float l (float (* 0.12 (min width height))))
  12. (def ^:const ^float h (float (* 0.88 (min width height))))
  13.  
  14. (def ^:const ^int steps-per-frame 10000)
  15.  
  16. (defn make-attractor
  17.   "Generates an de Jong attractor around the given parameters"
  18.   [^double a ^double b ^double c ^double d]
  19.   (fn attractor
  20.     [[^double x ^double y]]
  21.     [(- ^float (q/sin (* a y)) ^float (q/cos (* b x)))
  22.      (- ^float (q/sin (* c x)) ^float (q/cos (* d y)))]))
  23.  
  24. (defn setup []
  25.   (q/frame-rate 30)
  26.   (q/color-mode :hsb)
  27.   (q/background 20)
  28.   (q/fill 20)
  29.   (let [a -1.68661
  30.         b -1.99168
  31.         c 1.71743
  32.         d -1.64958]
  33.     {:attractor (iterate (make-attractor a b c d) [0 0])
  34.      :histogram (int-array (* width height) 0)})) ; amount of times each pixel has been 'visited'
  35.  
  36. (defn ->screen-space
  37.   "Takes a coordinate in de Jong space [-2, 2]x[-2, 2] and maps them to a range
  38.  from l to h."
  39.   [coord l h]
  40.   (map #(Math/round ^float (q/map-range % -2 2 l h)) coord))
  41.  
  42. (defn update-state [{:keys [attractor histogram] :as state}]
  43.   (let [coords (take steps-per-frame attractor)]
  44.     {:attractor (drop steps-per-frame attractor)
  45.      :histogram (loop [^ints histogram histogram
  46.                        coords (map #(->screen-space % l h) coords)]
  47.                   (if (empty? coords)
  48.                     histogram
  49.                     (let [[^int x ^int y] (first coords)]
  50.                       (recur
  51.                        (let [idx (+ (* y width) x)
  52.                              v (inc (aget histogram idx))]
  53.                          (aset-int histogram idx v)
  54.                          histogram)
  55.                        (rest coords)))))}))
  56.  
  57. (defn draw-state [{:keys [^ints histogram]}]
  58.   ;; clear drawing
  59.   (q/no-stroke)
  60.   (q/rect 0 0 (q/width) (q/height))
  61.   ;; draw all dots with alpha values from 0 to 255
  62.   (dotimes [y width]
  63.     (let [row (* y width)]
  64.       (dotimes [x height]
  65.         (let [idx (+ row x)]
  66.           (let [v (aget histogram idx)]
  67.             (when-not (zero? v)
  68.               (q/stroke 122 255 255 (* 50.0 ^float (q/log (inc v))))
  69.               (q/point x y))))))))
  70.  
  71. (q/defsketch de-jong
  72.   :title "De-Jong"
  73.   :size [width height]
  74.   :setup setup
  75.   :update update-state
  76.   :draw draw-state
  77.   :features [:keep-on-top]
  78.   :middleware [m/pause-on-error m/fun-mode])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement