Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## 8dots3Dtiltspin2.py >>> double buffer added
- import math,e32,appuifw,graphics,key_codes,time,sensor
- def draw(rect):
- try: c.blit(img)
- except: pass
- xm,ym,x2,y2 = 0,0,0,0
- def redraw():
- global xm,ym
- try: ym,xm,zm=(sens.x/10.0,sens.y/10.0,sens.z/10.0) ### Z axis is not needed
- except: pass
- def exit(pos):
- global run
- run=0
- run = 1
- img=graphics.Image.new((640,360))
- appuifw.app.directional_pad=False
- c=appuifw.Canvas(redraw_callback=draw)
- appuifw.app.body=c
- c.bind(key_codes.EButton1Down,exit)
- sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
- sens.set_callback(data_callback=redraw)
- sens.start_listening()
- appuifw.app.orientation='landscape'
- appuifw.app.screen='full'
- halfResX = c.size[0]/2
- halfResY = c.size[1]/2
- distance = 180
- PX = 0
- PY = 0
- PZ = 200
- Size = 10
- xyz=80
- VX = [-xyz, xyz, -xyz, xyz, -xyz, xyz, -xyz, xyz]
- VY = [-xyz, -xyz, xyz, xyz, -xyz, -xyz, xyz, xyz]
- VZ = [-xyz, -xyz, -xyz, -xyz, xyz, xyz, xyz, xyz]
- ANGLEX = 0
- ANGLEY = 0
- ANGLEZ = 0
- while run:
- rax = ANGLEX*math.pi/180
- ray = ANGLEY*math.pi/180
- raz = ANGLEZ*math.pi/180
- sinx = math.sin(rax)
- cosx = math.cos(rax)
- siny = math.sin(ray)
- cosy = math.cos(ray)
- sinz = math.sin(raz)
- cosz = math.cos(raz)
- buf=graphics.Image.new((640,360)) ### not sure if this is the pythonic double buffer, but it works like a charm
- img=graphics.Image.new((640,360))
- try:
- for n in record:
- x,y,z = n
- buf.point((x,y),0xffffff,width=z)
- except:pass
- buf.text((20,40),'X:'+unicode(x2),0xffffff)
- buf.text((20,80),'Y:'+unicode(y2),0xffffff)
- record=[]
- for n in range(8):
- ZX = VX[n]*cosz - VY[n]*sinz - VX[n]
- ZY = VX[n]*sinz + VY[n]*cosz - VY[n]
- YX = (VX[n]+ZX)*cosy - VZ[n]*siny - (VX[n]+ZX)
- YZ = (VX[n]+ZX)*siny + VZ[n]*cosy - VZ[n]
- XY = (VY[n]+ZY)*cosx - (VZ[n]+YZ)*sinx - (VY[n]+ZY)
- XZ = (VY[n]+ZY)*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
- XR = YX+ZX
- YR = ZY+XY
- ZR = XZ+YZ
- z = (VZ[n]+PZ+ZR)/distance
- x = ((VX[n]+PX+XR)/z)+halfResX
- y = ((VY[n]+PY+YR)/z)+halfResY
- z = int(Size/z)
- buf.point((int(x),int(y)),0x0000ff,width=z)
- record.append((x,y,z))
- buf.point((PX+halfResX,PY+halfResY),0xff0000,width=Size*3)
- buf.text((20,40),'X:'+unicode(xm),0x000000)
- buf.text((20,80),'Y:'+unicode(ym),0x000000)
- x2,y2=xm,ym
- ANGLEX+=ym
- ANGLEY+=xm
- img=buf
- draw(())
- wait=time.time()+0.005 ### ZZZ might flicker with a less wait
- while time.time() < wait:
- pass
- e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement