Skip to content

Instantly share code, notes, and snippets.

@adicirstei
Last active April 23, 2019 21:07
Show Gist options
  • Save adicirstei/d3c3c5d13e1c4a0bfbe99a9c6da4bff0 to your computer and use it in GitHub Desktop.
Save adicirstei/d3c3c5d13e1c4a0bfbe99a9c6da4bff0 to your computer and use it in GitHub Desktop.
Beads on vector fields source
(ns gen-art.beads
(:require [clojure2d.core :refer :all]
[fastmath.core :as m]
[fastmath.random :as r]
[fastmath.fields :as f]
[fastmath.vector :as v]
[clojure2d.color :as c]
[clojure2d.extra.utils :as ut]
[clojure2d.pixels :as p])
(:import [fastmath.vector Vec2]) )
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(def ^:const ^double sp3 (m/sin m/THIRD_PI))
(def ^:const ^long slotsx 110)
(def ^:const ^double w 900)
(def ^:const ^double h 400)
;; vector field dimensions
(def ^:const ^double bx (* 4.0 w (/ 1.0 (min h w))) )
(def ^:const ^double by (* 4.0 h (/ 1.0 (min h w))) )
;; center offset of vector field
(def ^:const ^Vec2 coff (Vec2. (- (/ bx 2.0)) (- (/ by 2)) ))
(def ^:const ^double edge (/ bx slotsx))
(def ^:const ^long slotsy (Math/round (/ h (* sp3 (/ w slotsx)) )))
(def pal (nth c/colourlovers-palettes (r/irand (count c/colourlovers-palettes) )))
(def bw-pal-1 [0x29292e 0xf9f9fa 0x9c9ca0 ])
(def bw-pal-2 [(v/vec4 30 30 30 255) (v/vec4 180 180 180 255) :white])
(def fld (f/combine (f/random-configuration 3)))
;;(def fld (f/field :swirl))
;(ut/show-palette bw-pal-1)
(def art-pal (nth c/colourlovers-palettes 146))
(defn p-grad [p x]
(let [c (- (count p) 1)
i (Math/round (* c x))
idx (if (> i c) c i)]
(nth p idx)))
(def grad (partial p-grad art-pal))
(defn idx-to-vec [^long idx]
(let [r (quot idx slotsx)
c (mod idx slotsx)
e (/ bx slotsx)
offset (if (odd? r) (* e 0.5) 0.0)
x (+ (* c e) offset)
y (* r sp3 e)
]
(Vec2. x y)))
(defn vec-to-idx [^Vec2 v]
(let [x (.x v)
y (.y v)
e (/ bx slotsx)
he (* e 0.5)
h (* sp3 e)
r (Math/round (/ y h))
x' (if (odd? r) (- x he) x)
c (Math/round (/ x' e))]
(+ c (* r slotsx))
))
(defn to-screenv [^Vec2 p]
(let [fact (/ w bx)]
(v/mult p fact)))
(defn field [^Vec2 p]
(let [v (v/mult (v/normalize (fld (v/add p coff))) edge)]
(v/add p v)))
(defn in-bounds [[^double x ^double y]]
(and (pos? x) (pos? y) (< x bx) (< y by)))
(defn step [p c sl]
(let [p' (field p)
i (vec-to-idx p')]
(if (and (in-bounds p') (contains? (set sl) i))
[c i p']
[(r/drand 0.0 1.0) (first sl) (idx-to-vec (first sl))])))
(defn make-me
[canvas window]
(loop [sl (shuffle (range (* slotsx slotsy)))
color (r/drand 0.0 1.0)
i (first sl)
p (idx-to-vec i)
]
(let [ps (to-screenv p)
sl' (filter #(not= i %) sl)
z (* 0.7 (/ w slotsx))]
(set-color canvas (grad color) )
(ellipse canvas (.x ps) (.y ps) z z )
(when (pos? (count sl'))
(let [[nc ni pp] (step p color sl')]
(recur sl' nc ni pp)))))
canvas)
(defn draw-something
""
[[canvas disp]]
(with-canvas-> canvas
(set-background 0 5 8)
(set-color 35 35 35 16)
(make-me disp)
:done))
(defn example-01
""
[]
(let [cnvs (canvas w h)
window (show-window cnvs "beads" (/ w 2) (/ h 2) 30 nil )]
(defmethod key-pressed ["beads" \space] [_ _]
(save cnvs (next-filename "results/beads/" ".jpg")))
[cnvs window]))
(draw-something (example-01))
(defproject gen-art "0.1.0-SNAPSHOT"
:description "FIXME: write description"
: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"]]
:main ^:skip-aot gen-art.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment