from math import pi def setup(): size(1000, 500) def draw(): # clear canvas to white background color background(255) # transform to scene coordinates, as pixel coordinates are annoying translate(0, 0.5+height/2.0) scale(width/10.0, -height/5.0) strokeWeight(0.02) # the number of frames per "scene" sceneframes = 1000 # the animation will cycle through 4 different "scenes" iscene = (frameCount/sceneframes)%6 # frame number within this scene isceneframe = frameCount%sceneframes # cook up a "time" that loops from tmin to tmax within each scene tmin = -1.0; tmax = +23.0; dt = (tmax-tmin)/sceneframes t = tmin + isceneframe*dt # speed of wave propagation vwave = 2.0 # wavelength etc. N = iscene + 1 Lambda = 20.0/N k = 2*pi/Lambda omega = vwave*k # loop over x axis positions to draw the wave function psi(x) xmin = 0.0; xmax = 10.0; dx = (xmax-xmin)/width xlast = 0.0; psilast = 0.0 for ix in range(width): x = xmin + ix*dx psi = sin(k*x) * sin(omega*t) if t<0 or t>22: psi = 0.0 # draw line segment from previous point to this point line(xlast, psilast, x, psi) # this point becomes "previous" point for next iteration xlast, psilast = x, psi