Skip to content

Instantly share code, notes, and snippets.

View postspectacular's full-sized avatar
🎉
Celebrating 25 years of building open source tools

Karsten Schmidt postspectacular

🎉
Celebrating 25 years of building open source tools
View GitHub Profile
@postspectacular
postspectacular / dejong.clj
Created June 26, 2016 13:12
Peter de Jong Strange Attractor
(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) x)))
(+ (Math/sin (* (* c x) x)) (Math/cos (* d y)))))
(->> (range 1e6)
(reduce
(fn [[points [x y]] _]
@postspectacular
postspectacular / polysample.clj
Created June 26, 2016 12:14
Polygon generation and resampling
(require '[thi.ng.geom.core :as g])
(require '[thi.ng.geom.circle :as c])
(require '[thi.ng.geom.polygon :as poly])
(-> (c/circle 100)
(g/as-polygon 6)
(g/sample-uniform 10 false))
;; [[100.0 0.0] [95.0 8.660254037844387] [90.0 17.320508075688775] ...]
@postspectacular
postspectacular / lsys.clj
Last active June 26, 2016 00:42
L-System interpreter
(require '[thi.ng.geom.core :as g])
(require '[thi.ng.geom.vector :as v])
(defn make-agent
[pos theta speed rot-theta]
{:pos pos
:theta theta
:rot-theta rot-theta
:speed speed
:path []
@postspectacular
postspectacular / canvas.cljs
Created May 1, 2016 23:20
Reagent canvas component
(ns canvas
(:require
[thi.ng.geom.gl.webgl.animator :as anim]
[reagent.core :as reagent]))
(defn canvas-component
[props]
(reagent/create-class
{:component-did-mount
(fn [this]
@postspectacular
postspectacular / get-in.clj
Created May 1, 2016 12:31
get-in macro version
(defmacro get-in*
"Macro version of clojure.core/get-in without not-found fallback"
[root path]
(loop [root root, path path]
(if path
(recur `(get ~root ~(first path)) (next path))
root)))
(macroexpand-1 '(get-in* [[1 2 3] [3 4 [5 6 7 8]]] [1 2 3]))
;; (clojure.core/get (clojure.core/get (clojure.core/get [[1 2 3] [3 4 [5 6 7 8]]] 1) 2) 3)
@postspectacular
postspectacular / core.clj
Created April 30, 2016 22:32
Updating C particle system & rendering w/ WebGL
(defn attrib-buffer-view
[ptr stride num]
(js/Float32Array. (.-buffer (aget js/Particles "HEAPU8")) ptr (* stride num)))
(defn update-attrib-buffer
[gl attrib ptr stride num]
(.bindBuffer gl glc/array-buffer
(get-in (:scene @app) [:particles :attribs attrib :buffer]))
(.bufferData gl glc/array-buffer
(attrib-buffer-view ptr stride num)
@postspectacular
postspectacular / core.cljs
Created April 30, 2016 22:24
Wrapping & calling compiled C fns from Clojurescript
;; initialize particle system and
;; create C function wrappers
(let [psys (.ccall js/Particles "initParticleSystem" "*"
#js ["number" "number" "number" "number"]
#js [10000 1000 0.0 -0.01 0.125])
psys-update (.cwrap js/Particles "updateParticleSystem" "*"
#js ["number"])
psys-count (.cwrap js/Particles "getNumParticles" "number"
#js ["number"])
particle-ptr (.ccall js/Particles "getParticlesPointer" "*"
// particle system structs
typedef struct {
float x,y,z;
} Vec3;
typedef struct {
Vec3 pos; // 12 bytes
Vec3 vel; // 12 bytes
Vec3 col; // 12 bytes
@postspectacular
postspectacular / main.c
Last active April 25, 2019 22:13
Simple double buffer manager for STM32
static Screen *screen;
int main(void) {
HAL_Init();
SystemClock_Config();
screen = ct_screen_init();
while (1) {
ct_screen_flip_buffers(screen);
// put normal drawing code here...
@postspectacular
postspectacular / tjunctions.clj
Last active March 4, 2016 21:24
T-Junction mesh repair for thi.ng/geom (0.0.908)
(ns tjunctions
(:require
[thi.ng.geom.core :as g]
[thi.ng.geom.core.utils :as gu]
[thi.ng.geom.line :as l]
[thi.ng.geom.gmesh :as gm]
[thi.ng.math.core :as m]))
(defn- edges-without-v
"Takes gmesh and vertex, returns lazyseq of all edges NOT related to v."