Skip to content

Commit

Permalink
rewrite as interpolate with a smoothing parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dgtized committed Mar 10, 2024
1 parent 1514c77 commit a9eedec
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/shimmers/math/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,23 @@
(let [f (lagrange-barycentric [(gv/vec2 1 2) (gv/vec2 2 1) (gv/vec2 3 3)])]
(map (fn [x] (gv/vec2 x (f x))) (range 0 4 0.25))))

(defn k-lerp [xs t]
(defn interpolate
"For `xs` values, smoothly interpolate using `t` in [0.0,1.0).
The parameter `k` in [0.0,1.0), determines how long to stay at each values."
[xs k t]
(let [n (count xs)
v (* n t)
a (mod (Math/floor (- v 0.01)) n)
k (* 0.5 (mod k 1.0))
v (* n (mod t 1.0))
a (if (tm/delta= t 1.0)
(dec n)
(mod (Math/floor v) n))
b (mod (inc a) n)]
[t xs [a b] (tm/mix* (nth xs a) (nth xs b) (* n (- t (/ a n))))]))
(tm/mix* (nth xs a) (nth xs b)
(tm/smoothstep* k (- 1.0 k) (* n (- t (/ a n)))))))

(comment
(map (fn [t] (k-lerp [0.2 0.7 0.4] t)) (tm/norm-range 20)))
(map (fn [t] (interpolate [0.2 0.7 0.4] 0.0 t)) (tm/norm-range 20))
(map (fn [t] (interpolate [0.2 2.0 0.7] 0.1 t)) (tm/norm-range 20))
(map (fn [t] (interpolate [0.2 2.0 0.7] 0.3 t)) (tm/norm-range 20))
(map (fn [t] (interpolate [0.2 2.0 0.7] 1.0 t)) (tm/norm-range 20)))

0 comments on commit a9eedec

Please sign in to comment.