Skip to content

Commit

Permalink
Add halston sequence example to random-points
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed Sep 27, 2023
1 parent 2fe2eb9 commit d569cc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/shimmers/algorithm/random_points.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns shimmers.algorithm.random-points
(:require
[shimmers.algorithm.poisson-disc-sampling :as pds]
[shimmers.math.core :as sm]
[shimmers.math.deterministic-random :as dr]
[thi.ng.geom.circle :as gc]
[thi.ng.geom.core :as g]
Expand Down Expand Up @@ -68,8 +69,35 @@
(reset! pool (if (seq r) r (dr/shuffle points)))
v))))

;; https://observablehq.com/@jrus/halton
(defn halton [index base]
(loop [index index
fraction 1
result 0]
(if (<= index 0)
result
(let [fract (/ fraction base)]
(recur (Math/floor (/ index base))
fract
(+ result (* fract (mod index base))))))))

(comment (for [i (range 32)]
(halton i 7)))

;; how to pick good prime pairs?
(defn halton-prime-pair [lower upper]
(let [primes (sm/primes-between lower upper)
p1 (dr/rand-nth primes)]
[p1 (dr/rand-nth (remove #{p1 (+ p1 2) (- p1 2)} primes))]))

(defn halton-sequence [bounds [p1 p2] n]
(for [i (range n)
:let [k (+ i 100)]]
(g/unmap-point bounds (gv/vec2 (halton k p1) (halton k p2)))))

(def modes
{:random-points random-points
:random-cells random-cells
:random-cell-jitter random-cell-jitter
:poisson-disc-sampling poisson-disc-sampling} )
:poisson-disc-sampling poisson-disc-sampling
:halton-sequence halton-sequence} )
8 changes: 7 additions & 1 deletion src/shimmers/sketches/random_point_field.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[shimmers.algorithm.random-points :as rp]
[shimmers.common.svg :as csvg]
[shimmers.common.ui.controls :as ctrl]
[shimmers.common.ui.debug :as debug]
[shimmers.math.graph :as graph]
[shimmers.sketch :as sketch :include-macros true]
[shimmers.view.sketch :as view-sketch]
Expand Down Expand Up @@ -46,7 +47,10 @@
(let [bounds (g/scale-size (rect/rect 0 0 width height) 0.99)
{:keys [mode n-points mst]} @ui-state
point-cloud (get rp/modes mode)
points (point-cloud bounds (or n-points 1))]
primes (rp/halton-prime-pair 20 60)
points (if (= mode :halton-sequence)
(point-cloud bounds primes (* n-points 4))
(point-cloud bounds (or n-points 1)))]
[:div
[:div.canvas-frame [scene points mst]]
[:div.explanation.contained
Expand All @@ -59,6 +63,8 @@
[:h4 "Controls"]
(ctrl/change-mode ui-state (keys rp/modes))
(ctrl/numeric ui-state "Generated Points" [:n-points] [2 1024 1])
(when (= mode :halton-sequence)
[:div "Halton sequence from co-primes " (debug/pre-edn primes)])
(ctrl/checkbox ui-state "Show MST Circles" [:mst])]]]]))

(sketch/definition random-point-field
Expand Down

0 comments on commit d569cc5

Please sign in to comment.