Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #to run, use processing 3 in python mode. Also make sure this file is saved as "doublePendulum.pyde" in a folder called "doublePendulum"
- class DoublePendulum:
- def __init__(self,th1,th2,g=1,dt=1):
- self.th1=th1%TWO_PI
- self.th2=th2%TWO_PI
- self.om1=0
- self.om2=0
- self.g=g
- self.dt=dt
- def step(self):
- temp=-3*self.g*sin(self.th1)
- temp-=self.g*sin(self.th1-2*self.th2)
- temp-=2*sin(self.th1-self.th2)*(self.om2**2+cos(self.th1-self.th2)*self.om1**2)
- omPrime1=temp/(3-cos(2*self.th1-2*self.th2))
- temp=2*self.om1**2+2*self.g*cos(self.th1)+cos(self.th1-self.th2)*self.om2**2
- omPrime2=temp*2*sin(self.th1-self.th2)/(3-cos(2*self.th1-2*self.th2))
- self.om1+=self.dt*omPrime1
- self.om2+=self.dt*omPrime2
- self.th1=(self.th1+self.dt*self.om1)%TWO_PI
- self.th2=(self.th2+self.dt*self.om2)%TWO_PI
- def show(self,x0,y0,r=20,c=color(255)):
- stroke(c)
- strokeWeight(2)
- x1,y1=x0+r*cos(self.th1+HALF_PI),y0+r*sin(self.th1+HALF_PI)
- x2,y2=x1+r*cos(self.th2+HALF_PI),y1+r*sin(self.th2+HALF_PI)
- line(x0,y0,x1,y1)
- line(x1,y1,x2,y2)
- noStroke()
- fill(255)
- ellipse(x1,y1,3,3)
- ellipse(x2,y2,3,3)
- def setup():
- global dp,gridSize
- size(728,728)
- #size(200,200)
- colorMode(HSB)
- gridSize=width
- dp=[[DoublePendulum(TWO_PI*(1-float(i)/gridSize),TWO_PI*j/gridSize,dt=0.01) for j in range(gridSize)] for i in range(gridSize)]
- def draw():
- background(255)
- img=createImage(gridSize,gridSize,HSB)
- img.loadPixels()
- for i,row in enumerate(dp):
- for j,p in enumerate(row):
- p.step()
- x=cos(p.th1)+cos(p.th2)
- y=sin(p.th1)+sin(p.th2)
- if x==0: arg=HALF_PI
- else: arg=atan(y/x)
- r=sqrt(x**2+y**2)
- img.pixels[i+j*img.width]=color((arg%PI)*255/PI,255,r*255/2)
- img.updatePixels()
- image(img,0,0)
- #dp[mouseX][mouseY].show(mouseX,mouseY) #show's a tiny pendulum underneath the mouse cursor for the pixel the cursor is over
- #saveFrame("frames\######.png")
Advertisement
Add Comment
Please, Sign In to add comment