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 = 800 # frame number within this scene isceneframe = frameCount%sceneframes # cook up a "time" that loops from tmin to tmax within each scene tmin = +2.0; tmax = +42.0; dt = (tmax-tmin)/sceneframes t = tmin + isceneframe*dt # speed of wave propagation vwave = 1.0 # width of gaussian wave packet sigma = 0.5 # positions of the two wave packets' centers x1 = vwave*t x2 = vwave*(20-t) x3 = vwave*(t-20) x4 = vwave*(40-t) x5 = vwave*(t-40) # 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 = +1.0 * exp(-0.5*((x-x1)/sigma)**2) psi2 = +1.0 * exp(-0.5*((x-x2)/sigma)**2) psi3 = -1.0 * exp(-0.5*((x-x3)/sigma)**2) psi4 = -1.0 * exp(-0.5*((x-x4)/sigma)**2) psi5 = +1.0 * exp(-0.5*((x-x5)/sigma)**2) psi = psi1 + psi2 + psi3 + psi4 + psi5 # 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