From 823b3399c473e594726023c78eb032e54ba4e89e Mon Sep 17 00:00:00 2001 From: Charles Comstock Date: Mon, 29 Apr 2024 16:03:48 -0500 Subject: [PATCH] add lcm/gcd as options for c-multiple in helix sketch --- src/shimmers/math/core.cljc | 9 +++++++++ src/shimmers/sketches/helix.cljs | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/shimmers/math/core.cljc b/src/shimmers/math/core.cljc index 1537c44a3..9e03f5aed 100644 --- a/src/shimmers/math/core.cljc +++ b/src/shimmers/math/core.cljc @@ -116,6 +116,15 @@ (comment (map (fn [n] [n (factors n 9)]) (range 1 100))) +;; https://rosettacode.org/wiki/Least_common_multiple#Clojure +(defn gcd [a b] + (if (zero? b) + a + (recur b (mod a b)))) + +(defn lcm [a b] + (/ (* a b) (gcd a b))) + ;; https://en.wikipedia.org/wiki/Lagrange_polynomial ;; https://www.youtube.com/watch?v=4S6G-zenbFM (defn lagrange-interpolate [points] diff --git a/src/shimmers/sketches/helix.cljs b/src/shimmers/sketches/helix.cljs index 504667453..911d3aca0 100644 --- a/src/shimmers/sketches/helix.cljs +++ b/src/shimmers/sketches/helix.cljs @@ -4,6 +4,7 @@ [shimmers.common.svg :as csvg :include-macros true] [shimmers.common.ui.controls :as ctrl] [shimmers.common.ui.debug :as debug] + [shimmers.math.core :as sm] [shimmers.math.deterministic-random :as dr] [shimmers.math.equations :as eq] [shimmers.math.vector :as v] @@ -75,7 +76,10 @@ {:n-points 2048 :a (* (dr/weighted {-1 1 1 6}) a) :b (* (dr/weighted {-1 6 1 1}) b) - :c (* (min a b) (dr/random-int 1 6)) + :c (dr/weighted + [[(* (min a b) (dr/random-int 1 6)) 2.0] + [(sm/lcm a b) 1.0] + [(sm/gcd a b) 1.0]]) :a-osc (* (dr/random-sign) (dr/random-int 4)) :b-osc (* (dr/random-sign) (dr/random-int 6)) :c-osc (* (dr/random-sign) (dr/random-int 12))