Skip to content

Instantly share code, notes, and snippets.

@adicirstei
Created February 7, 2019 14:56
Show Gist options
  • Save adicirstei/8a602e4c5be7feba1f86a464abdee662 to your computer and use it in GitHub Desktop.
Save adicirstei/8a602e4c5be7feba1f86a464abdee662 to your computer and use it in GitHub Desktop.
Peter de Jong attractor
(ns attractors.dejong
(:require [clojure2d.core :refer :all]
[clojure2d.pixels :as p]
[clojure2d.color :as c]
[fastmath.core :as m]
[fastmath.vector :as v]
[fastmath.random :as r]))
;(set! *warn-on-reflection* true)
;(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(def ^:const ^int size 800)
(def ^:const ^double gamma (/ 0.85))
(def ^:const ^double cgamma (/ 2.0))
(def ^:const ^double vibrancy 0.5)
(def ^:const ^double brightness 1.6)
(def ^:const ^double contrast 1.2)
(def ^:const ^double saturation 1.2)
(def ^:const ^int steps 1000000)
(def col (c/gradient [ 0x1dc75a 0xffeb3b 0x6699cc 0x1dc75a 0xffeb3b 0x6699cc 0x1dc75a 0xffeb3b 0x6699cc]))
(def center (v/vec2 (/ size 2) (/ size 2)))
(def wscale (/ size 4))
(defn compute-dejong
"Computes a single DeJong 2d point vector for given params and XY pos"
[a b c d x y]
(v/vec2
(- (Math/sin (* a y)) (Math/cos (* b x)))
(- (Math/sin (* c x)) (Math/cos (* d y)))))
(def dejong
(let [a (r/drand -3 3)
b (r/drand -3 3)
c (r/drand -3 3)
d (r/drand -3 3)]
(println (str "a: " a " b: " b " c: " c " d: " d))
(partial compute-dejong a b c d)))
(defn dejong-seq
[f x y]
(let [v (f x y)
[x' y'] v]
(lazy-seq
(cons v (dejong-seq f x' y')))))
(defn to-idxs
[v]
(mapv int (-> v
(v/mult wscale)
(v/add center))))
(defn draw-dejong []
(let [b (p/renderer size size :hann)
x (r/grand)
y (r/grand)
s (map to-idxs (dejong-seq dejong x y))]
(doseq [[px py] (take steps s)]
(let [t1 (r/noise x y)
t2 (r/noise (- y 1.1) (- x 1.1) 0.4)
t (m/sqrt (* t1 t2))]
(p/set-color b px py (col t))))
b))
(def c (canvas size size))
(def buff (p/renderer size size))
(def window (show-window {:window-name "DeJong"
:canvas c}))
(defmethod key-pressed ["DeJong" \space] [_ _]
(save c (next-filename "results/dejong-" ".jpg")))
(loop [iter (int 1)]
(let [ts (mapv (fn [_] (future (draw-dejong))) (range available-cores))]
(apply p/merge-renderers buff (map deref ts))
(println (str "Points: " (* available-cores iter steps)))
(p/set-canvas-pixels! c (p/to-pixels buff {:background (c/gray 15)
:gamma-alpha gamma
:gamma-color cgamma
:vibrancy vibrancy
; :brightness brightness
; :contrast contrast
:saturation saturation}))
)
(when (window-active? window) (recur (inc iter))))
(defproject attractors "0.1.0-SNAPSHOT"
:description "a clojure2d attractors implementations"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[clojure2d "1.2.0-SNAPSHOT"]]
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
@adicirstei
Copy link
Author

dejong-1
dejong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment