Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pendulum
- # joe, 03/01/2015
- import appuifw as A
- import graphics as G
- import math
- def radians(degrees):
- return degrees*math.pi/180.0
- sin,cos=math.sin,math.cos
- class Pend:
- def __init__(self):
- self.theta=45
- self.dtheta=0
- self.rect = (PIVOT[0]-LENGTH*cos(radians(self.theta)),PIVOT[1] + LENGTH*sin(radians(self.theta)),1,1)
- def compute(self):
- scaling = 3000.0 / (LENGTH**2)
- firstDDtheta = -sin(radians(self.theta)) * scaling
- midDtheta = self.dtheta + firstDDtheta
- midtheta = self.theta + (self.dtheta + midDtheta)/2.0
- midDDtheta = -sin(radians(midtheta))*scaling
- midDtheta = self.dtheta + (firstDDtheta + midDDtheta)/2
- midtheta = self.theta + (self.dtheta + midDtheta)/2
- midDDtheta = -sin(radians(midtheta)) * scaling
- lastDtheta = midDtheta+midDDtheta
- lasttheta = midtheta + (midDtheta + lastDtheta)/2.0
- lastDDtheta = -sin(radians(lasttheta)) * scaling
- lastDtheta = midDtheta + (midDDtheta + lastDDtheta)/2.0
- lasttheta = midtheta + (midDtheta + lastDtheta)/2.0
- self.dtheta = lastDtheta
- self.theta = lasttheta
- self.rect = (PIVOT[0]- LENGTH*sin(radians(self.theta)),PIVOT[1] + LENGTH*cos(radians(self.theta)),1,1)
- def update(self,g):
- self.compute()
- self.draw(g)
- def draw(self,x):
- cx,cy=self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2
- x.line((5,PIVOT[1],W-5,PIVOT[1]),0xffffff)
- x.line((PIVOT[0],PIVOT[1],cx,cy),0xffffff)
- x.point((PIVOT[0],PIVOT[1],5,0),0x00ff00,width=4)
- x.point((cx,cy),0x00ff00,width=SIZE)
- class App:
- def __init__(self):
- self.run=0
- A.app.screen='full'
- self.c = A.Canvas(redraw_callback=self.redraw,event_callback=None)
- A.app.body=self.c
- A.app.exit_key_handler = self.stop
- def stop(self):
- self.run=0
- def redraw(self,x):
- if BG:
- self.c.blit(BG)
- def play(self):
- self.run=1
- while self.run:
- BG.clear(0)
- BG.text((5,H-8),u'Pendulum',0xffffff)
- pen.update(BG)
- self.redraw(0)
- A.e32.ao_sleep(1e-04)
- W,H=G.sysinfo.display_pixels()
- PIVOT=(W/2,H/8)
- LENGTH,SIZE=PIVOT[1]*5,15
- BG=G.Image.new((W,H))
- pen=Pend()
- App().play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement