|
| 1 | +using GLMakie |
| 2 | + |
| 3 | +function figure_eight_path(A, B, C, D, x0, y0, theta, num_points) |
| 4 | + t = range(0, 2π, length=num_points) |
| 5 | + x = A * sin.(t) |
| 6 | + y = B * sin.(t) .* cos.(t) .+ C .* cos.(t) .+ D .* cos.(2t) |
| 7 | + # Apply rotation |
| 8 | + x_rot = x .* cos(theta) .- y .* sin(theta) |
| 9 | + y_rot = x .* sin(theta) .+ y .* cos(theta) |
| 10 | + y_rot = y_rot / (maximum(y_rot)/(B/2)) |
| 11 | + # Apply translation |
| 12 | + x_final = x_rot .+ x0 |
| 13 | + y_final = y_rot .+ y0 |
| 14 | + return x_final, y_final |
| 15 | +end |
| 16 | + |
| 17 | +# Example usage: |
| 18 | +A = 10.0 # width of the figure-eight |
| 19 | +B = 5.0 # height of the figure-eight |
| 20 | +C = Observable(1.0) # size of right part of the figure-eight |
| 21 | +D = Observable(1.0) # asymmetry factor |
| 22 | +x0 = 0.0 # center x-coordinate |
| 23 | +y0 = 0.0 # center y-coordinate |
| 24 | +theta = 0*π/6 # rotation angle in radians |
| 25 | +num_points = 200 |
| 26 | + |
| 27 | +# Define the x range |
| 28 | +x = LinRange(0, 6π, 500) |
| 29 | + |
| 30 | +# Observable for the y values |
| 31 | +y = @lift($C .* sin.($D .* x)) |
| 32 | + |
| 33 | +# Create the figure and axis |
| 34 | +fig = Figure() |
| 35 | +ax = Axis(fig[1, 1], xlabel = "x", ylabel = "y") |
| 36 | + |
| 37 | +# Plot the sine wave |
| 38 | +lineplot = lines!(ax, x, y) |
| 39 | + |
| 40 | +# Create sliders for amplitude and frequency |
| 41 | +sg = SliderGrid( |
| 42 | + fig[2, 1], |
| 43 | + (label = "C", range = 0.0:0.01:2.0, startvalue = 1.0), |
| 44 | + (label = "D", range = 0.5:0.01:3.0, startvalue = 1.0) |
| 45 | +) |
| 46 | + |
| 47 | +# Connect sliders to observables |
| 48 | +on(sg.sliders[1].value) do val |
| 49 | + C[] = val |
| 50 | +end |
| 51 | + |
| 52 | +on(sg.sliders[2].value) do val |
| 53 | + D[] = val |
| 54 | +end |
| 55 | + |
| 56 | +# Optional: keep axis limits stable if amplitude changes a lot |
| 57 | +# ax.ylims = (-2, 2) # or use autolimits! if you prefer dynamic limits |
| 58 | + |
| 59 | +fig |
0 commit comments