Skip to content

Instantly share code, notes, and snippets.

@alexgian
Last active July 14, 2024 01:31
Show Gist options
  • Select an option

  • Save alexgian/2bd9a8ec329ad8fa9cf815eb3a36e41a to your computer and use it in GitHub Desktop.

Select an option

Save alexgian/2bd9a8ec329ad8fa9cf815eb3a36e41a to your computer and use it in GitHub Desktop.
(ns strake
(:refer-clojure
:exclude [* + - / = abs compare denominator divide
infinite? numerator partial ref zero?])
(:require
[emmy.env :refer :all]
[emmy.leva :as leva]
[emmy.mathbox :as box]
[emmy.mathbox.plot :as plot]
[emmy.mathbox.physics :as phys]
[emmy.viewer :as ev]
)
)
(def u [0 (* pi 2)])
(def v [-1 1])
(def vscale 1)
(def u2 (* v pi))
(def t [0 1])
(def IR "inside radius"1)
(def OR "outer radius" 2)
(def Coils 3)
(def Compr 1)
(defn cylinder
"standard parametric equation of a cylinder,
(corresponding to the one for cylindrical coordinates)"
[radius v-scale]
(fn [[u v]]
[(* radius (cos u))
(* radius (sin u))
(* v-scale v)]))
(defn strake1
""
[in-radius out-radius height coils stretch?]
(fn [[u t]]
(let [h1 (if (= stretch? 'true) height 1)
h2 (if (= stretch? 'false) 1 height)
strake-band (+ (* (- 1 t) in-radius) (* t out-radius))
u1 (* coils h1 u)]
[(* (cos u1) strake-band)
(* (sin u1) strake-band)
(/ (* u1 h2) (* pi coils))])))
(defn strake2
""
[in-radius out-radius height coils stretch?]
(fn [[u1 t]]
(let [h1 (if (= stretch? 'true) 1 height)
h2 (if (= stretch? 'false) height 1)
strake-band (+ (* (- 1 t) in-radius) (* t out-radius))
u (* coils h1 u1)]
[(* (cos u) strake-band)
(* (sin u) strake-band)
(/ (* u h2) (* pi coils))])))
(ev/with-let [!state {:inrad IR :outrad OR :height vscale
:coils Coils :stretch1 'true}]
(let [cylinder (plot/parametric-surface
{:u u :v v
:f (ev/with-params {:atom !state :params [:inrad :height]}
cylinder)
:color :blue})
strake (plot/parametric-surface
{:u u2 :v t
:f (ev/with-params {:atom !state
:params [:inrad :outrad :height :coils :stretch1]} strake1)
:color :cyan :u-samples 256})]
[:<> (leva/controls
{:folder {:name "Strake"}
:schema {:inrad {:min 0.5 :max 2 :step 0.1}
:outrad {:min 1 :max 4 :step 0.1}
:height {:min 1 :max 6 :step 0.1}
:coils {:min 1 :max 7 :step 0.5}}
:atom !state})
(plot/scene cylinder strake)]))
(ev/with-let [!state {:inrad IR :outrad OR :height vscale
:coils Coils :stretch2 'false}]
(let [cylinder (plot/parametric-surface
{:u u :v v
:f (ev/with-params {:atom !state :params [:inrad :height]}
cylinder)
:color :blue})
strake (plot/parametric-surface
{:u u2 :v t
:f (ev/with-params {:atom !state
:params [:inrad :outrad :height :coils :stretch2]} strake2)
:color :magenta :u-samples 2048})]
[:<> (leva/controls ; no need to repeat same schema
{:folder {:name "Strake"}
:schema
{:inrad {:min 0.5 :max 2 :step 0.1}
:outrad {:min 1 :max 4 :step 0.1}
:height {:min 1 :max 6 :step 0.1}
:coils {:min 1 :max 7 :step 0.5}}
:atom !state})
(plot/scene cylinder strake)]))
((strake2 'in-radius 'out-radius 'height 'coils 'false) [1 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment