CalculusWithJuliaNotes.jl/CwJ/makie-demos/spirograph.jl
2022-05-24 13:51:49 -04:00

50 lines
1018 B
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AbstractPlotting
using AbstractPlotting.MakieLayout
using GLMakie
# GUI for spirograph
# https://en.wikipedia.org/wiki/Spirograph
function x(t; R=1, k=1/4, l=1/4)
R*[(1-k)*cos(t) + l*k*cos((1-k)/k*t), (1-k)*sin(t) - l*k*sin((1-k)/k*t)]
end
# where we lay our scene:
scene, layout = layoutscene()
flyt = GridLayout()
flyt.halign[] = :left # fails?
flyt.valign[] = :top
layout[1,1] = flyt
p = layout[1,2] = LAxis(scene)
rowsize!(layout, 1, Relative(1))
colsize!(layout, 2, Relative(2/3))
flyt[1,1] = LText(scene, "t")
ts = flyt[1,2] = LSlider(scene, range = 2pi:pi/8:40pi)
flyt[2, 1] = LText(scene, "k = r/R")
k = flyt[2,2] = LSlider(scene, range = 0.01:0.01:1.0, startvalue=1/4)
flyt[3,1] = LText(scene, "l=ρ/r")
l = flyt[3,2] = LSlider(scene, range = 0.01:0.01:1.0, startvalue=1/4)
data = lift(ts.value, k.value, l.value) do ts,k,l
ts = range(0, ts, length=1000)
xys = Point2f0.(x.(ts, R=1, k=k, l=l))
end
lines!(p, data)
xlims!(p, (-1, 1))
ylims!(p, (-1, 1))
scene