Guest User

doublePendulum

a guest
Feb 20th, 2018
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. #to run, use processing 3 in python mode. Also make sure this file is saved as "doublePendulum.pyde" in a folder called "doublePendulum"
  2. class DoublePendulum:
  3. def __init__(self,th1,th2,g=1,dt=1):
  4. self.th1=th1%TWO_PI
  5. self.th2=th2%TWO_PI
  6. self.om1=0
  7. self.om2=0
  8. self.g=g
  9. self.dt=dt
  10. def step(self):
  11. temp=-3*self.g*sin(self.th1)
  12. temp-=self.g*sin(self.th1-2*self.th2)
  13. temp-=2*sin(self.th1-self.th2)*(self.om2**2+cos(self.th1-self.th2)*self.om1**2)
  14. omPrime1=temp/(3-cos(2*self.th1-2*self.th2))
  15. temp=2*self.om1**2+2*self.g*cos(self.th1)+cos(self.th1-self.th2)*self.om2**2
  16. omPrime2=temp*2*sin(self.th1-self.th2)/(3-cos(2*self.th1-2*self.th2))
  17.  
  18. self.om1+=self.dt*omPrime1
  19. self.om2+=self.dt*omPrime2
  20. self.th1=(self.th1+self.dt*self.om1)%TWO_PI
  21. self.th2=(self.th2+self.dt*self.om2)%TWO_PI
  22.  
  23. def show(self,x0,y0,r=20,c=color(255)):
  24. stroke(c)
  25. strokeWeight(2)
  26. x1,y1=x0+r*cos(self.th1+HALF_PI),y0+r*sin(self.th1+HALF_PI)
  27. x2,y2=x1+r*cos(self.th2+HALF_PI),y1+r*sin(self.th2+HALF_PI)
  28. line(x0,y0,x1,y1)
  29. line(x1,y1,x2,y2)
  30. noStroke()
  31. fill(255)
  32. ellipse(x1,y1,3,3)
  33. ellipse(x2,y2,3,3)
  34.  
  35. def setup():
  36. global dp,gridSize
  37. size(728,728)
  38. #size(200,200)
  39. colorMode(HSB)
  40. gridSize=width
  41. 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)]
  42. def draw():
  43. background(255)
  44. img=createImage(gridSize,gridSize,HSB)
  45. img.loadPixels()
  46. for i,row in enumerate(dp):
  47. for j,p in enumerate(row):
  48. p.step()
  49. x=cos(p.th1)+cos(p.th2)
  50. y=sin(p.th1)+sin(p.th2)
  51. if x==0: arg=HALF_PI
  52. else: arg=atan(y/x)
  53. r=sqrt(x**2+y**2)
  54. img.pixels[i+j*img.width]=color((arg%PI)*255/PI,255,r*255/2)
  55. img.updatePixels()
  56. image(img,0,0)
  57. #dp[mouseX][mouseY].show(mouseX,mouseY) #show's a tiny pendulum underneath the mouse cursor for the pixel the cursor is over
  58. #saveFrame("frames\######.png")
Advertisement
Add Comment
Please, Sign In to add comment