Advertisement
phjoe

Pendulum

Jan 3rd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # pendulum
  2. # joe, 03/01/2015
  3. import appuifw as A
  4. import graphics as G
  5. import math
  6.  
  7. def radians(degrees):
  8.  return degrees*math.pi/180.0
  9. sin,cos=math.sin,math.cos
  10.  
  11. class Pend:
  12.  def __init__(self):
  13.   self.theta=45
  14.   self.dtheta=0
  15.   self.rect = (PIVOT[0]-LENGTH*cos(radians(self.theta)),PIVOT[1] + LENGTH*sin(radians(self.theta)),1,1)
  16.  
  17.  def compute(self):
  18.   scaling = 3000.0 / (LENGTH**2)
  19.   firstDDtheta = -sin(radians(self.theta)) * scaling
  20.   midDtheta = self.dtheta + firstDDtheta
  21.   midtheta = self.theta + (self.dtheta + midDtheta)/2.0
  22.   midDDtheta = -sin(radians(midtheta))*scaling
  23.   midDtheta = self.dtheta + (firstDDtheta + midDDtheta)/2
  24.   midtheta = self.theta + (self.dtheta + midDtheta)/2
  25.   midDDtheta = -sin(radians(midtheta)) * scaling
  26.   lastDtheta = midDtheta+midDDtheta
  27.   lasttheta = midtheta + (midDtheta + lastDtheta)/2.0
  28.   lastDDtheta = -sin(radians(lasttheta)) * scaling
  29.   lastDtheta = midDtheta + (midDDtheta + lastDDtheta)/2.0
  30.   lasttheta = midtheta + (midDtheta + lastDtheta)/2.0
  31.   self.dtheta = lastDtheta
  32.   self.theta = lasttheta
  33.   self.rect = (PIVOT[0]- LENGTH*sin(radians(self.theta)),PIVOT[1] + LENGTH*cos(radians(self.theta)),1,1)
  34.  
  35.  def update(self,g):
  36.   self.compute()
  37.   self.draw(g)
  38.  
  39.  def draw(self,x):
  40.   cx,cy=self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2
  41.   x.line((5,PIVOT[1],W-5,PIVOT[1]),0xffffff)
  42.   x.line((PIVOT[0],PIVOT[1],cx,cy),0xffffff)
  43.   x.point((PIVOT[0],PIVOT[1],5,0),0x00ff00,width=4)
  44.   x.point((cx,cy),0x00ff00,width=SIZE)
  45.  
  46.  
  47. class App:
  48.  def __init__(self):
  49.   self.run=0
  50.   A.app.screen='full'
  51.   self.c = A.Canvas(redraw_callback=self.redraw,event_callback=None)
  52.   A.app.body=self.c
  53.   A.app.exit_key_handler = self.stop
  54.  
  55.  def stop(self):
  56.   self.run=0
  57.  
  58.  def redraw(self,x):
  59.   if BG:
  60.    self.c.blit(BG)
  61.  
  62.  def play(self):
  63.   self.run=1
  64.   while self.run:
  65.    BG.clear(0)
  66.    BG.text((5,H-8),u'Pendulum',0xffffff)
  67.    pen.update(BG)
  68.    self.redraw(0)
  69.    A.e32.ao_sleep(1e-04)
  70.  
  71.  
  72. W,H=G.sysinfo.display_pixels()
  73. PIVOT=(W/2,H/8)
  74. LENGTH,SIZE=PIVOT[1]*5,15
  75. BG=G.Image.new((W,H))
  76.  
  77.  
  78. pen=Pend()
  79. App().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement