Lissajous Figures
Edit the MicroSim with the p5.js Editor
Prompt
Create a p5.js animation of Lissajous figures.
Make one slider control the x-axis and one
slider control the y-axis.
Draw the figure on an aliceblue background.
ChatGPT o1-mini Response
Sure! Below is a p5.js sketch that animates Lissajous figures with two sliders controlling the frequencies of the x-axis and y-axis respectively.
Lissajous Figures Overview
Lissajous figures are complex harmonic motion patterns formed by combining two perpendicular oscillations. They are defined by the parametric equations:
where:
- A, B: Amplitudes in the x and y directions.
- a, b: Frequencies in the x and y directions.
- δ: Phase shift between the two oscillations.
In this sketch:
- Slider 1 controls the frequency
a
of the x-axis. - Slider 2 controls the frequency
b
of the y-axis.
Setting Up the Environment
To run this sketch:
- Visit the p5.js online editor.
- Create a new sketch.
- Replace the existing code with the code provided below.
- Click the play button to see the animation.
p5.js Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
Explanation of the Code
-
Variables Declaration:
-
sliderXFreq
andsliderYFreq
: Slider elements controlling the frequencies. xFreq
andyFreq
: Current frequencies for x and y axes.phase
: Phase shift to animate the figure.-
phaseSpeed
: Speed at which the phase shifts over time. -
setup()
Function: -
Initializes the canvas.
- Creates two sliders for controlling the x and y frequencies, each ranging from 1 to 10 with an initial value.
-
Sets the stroke weight and disables fill for drawing the Lissajous figure.
-
draw()
Function: -
Clears the background each frame to redraw the figure.
- Retrieves the current values from the sliders to update
xFreq
andyFreq
. - Defines amplitudes
A
andB
based on canvas size. - Translates the origin to the center of the canvas.
- Begins a new shape and iterates through values of
t
to plot the Lissajous figure using the parametric equations. - Increments the
phase
to animate the figure over time.
Customization
- Amplitude Adjustment: You can add sliders to control the amplitudes
A
andB
if desired. - Phase Control: Introduce another slider to adjust the phase shift
delta
manually. - Colors and Styles: Modify
stroke()
andstrokeWeight()
to change the appearance of the figure. - Resolution: Adjust the increment in the
for
loop (t += 0.01
) for smoother or faster rendering.
Live Demo
For a live demo, you can copy the above code into the p5.js online editor and see the animation in action. Adjust the sliders to explore different Lissajous patterns!