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 = 500 # the animation will cycle through 4 different "scenes" iscene = (frameCount/sceneframes)%4 # frame number within this scene isceneframe = frameCount%sceneframes # amplitude of first wave is always +1.0 A1 = 1.0 # amplitude of second wave varies scene by scene A2 = [1.0, -1.0, 0.5, -0.5][iscene] # cook up a "time" that loops from tmin to tmax within each scene tmin = -1.0; tmax = +11.0; dt = (tmax-tmin)/sceneframes t = tmin + isceneframe*dt # speed of wave propagation vwave = 1.0 # width of gaussian wave packet sigma = 0.2 # positions of the two wave packets' centers x1 = vwave*t x2 = vwave*(10-t) # 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 # calculate the two wave functions psi1(x) and psi2(x) psi1 = A1 * exp(-0.5*((x-x1)/sigma)**2) psi2 = A2 * exp(-0.5*((x-x2)/sigma)**2) psi = psi1 + psi2 # 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