Advertisement
here2share

# 8dots3Dtiltspin.py

Dec 25th, 2014
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 8dots3Dtiltspin.py
  2.  
  3. import math,e32,appuifw,graphics,key_codes,time,sensor
  4.  
  5. def draw(rect):
  6.     try: c.blit(img)
  7.     except: pass
  8.  
  9. xm,ym,x2,y2 = 0,0,0,0
  10. def redraw():
  11.     global xm,ym
  12.     try: ym,xm,zm=(sens.x/20.0,sens.y/20.0,sens.z/20.0) ### Z axis is not needed
  13.     except: pass
  14.  
  15. def exit(pos):
  16.     global run
  17.     run=0
  18.  
  19. run = 1
  20. img=graphics.Image.new((640,360))
  21. appuifw.app.directional_pad=False
  22. c=appuifw.Canvas(redraw_callback=draw)
  23. appuifw.app.body=c
  24. c.bind(key_codes.EButton1Down,exit)
  25. sens=sensor.AccelerometerXYZAxisData(data_filter=sensor.LowPassFilter())
  26. sens.set_callback(data_callback=redraw)
  27. sens.start_listening()
  28. appuifw.app.orientation='landscape'
  29. appuifw.app.screen='full'
  30.  
  31. halfResX = c.size[0]/2
  32. halfResY = c.size[1]/2
  33. distance = 180
  34.  
  35. PX = 0
  36. PY = 0
  37. PZ = 200
  38. Size = 10
  39. xyz=80
  40. VX = [-xyz, xyz, -xyz, xyz, -xyz, xyz, -xyz, xyz]
  41. VY = [-xyz, -xyz, xyz, xyz, -xyz, -xyz, xyz, xyz]
  42. VZ = [-xyz, -xyz, -xyz, -xyz, xyz, xyz, xyz, xyz]
  43. ANGLEX = 0
  44. ANGLEY = 0
  45. ANGLEZ = 0
  46.  
  47. while run:
  48.     rax = ANGLEX*math.pi/180
  49.     ray = ANGLEY*math.pi/180
  50.     raz = ANGLEZ*math.pi/180
  51.  
  52.     sinx = math.sin(rax)
  53.     cosx = math.cos(rax)
  54.     siny = math.sin(ray)
  55.     cosy = math.cos(ray)
  56.     sinz = math.sin(raz)
  57.     cosz = math.cos(raz)
  58.  
  59.     try:
  60.         for n in record:
  61.             x,y,z = n
  62.             img.point((x,y),0xffffff,width=z)
  63.     except:pass
  64.  
  65.     img.text((20,40),'X:'+unicode(x2),0xffffff)
  66.     img.text((20,80),'Y:'+unicode(y2),0xffffff)
  67.     record=[]
  68.     for n in range(8):
  69.         ZX = VX[n]*cosz - VY[n]*sinz - VX[n]
  70.         ZY = VX[n]*sinz + VY[n]*cosz - VY[n]
  71.         YX = (VX[n]+ZX)*cosy - VZ[n]*siny - (VX[n]+ZX)
  72.         YZ = (VX[n]+ZX)*siny + VZ[n]*cosy - VZ[n]
  73.         XY = (VY[n]+ZY)*cosx - (VZ[n]+YZ)*sinx - (VY[n]+ZY)
  74.         XZ = (VY[n]+ZY)*sinx + (VZ[n]+YZ)*cosx - (VZ[n]+YZ)
  75.         XR = YX+ZX
  76.         YR = ZY+XY
  77.         ZR = XZ+YZ
  78.         z = (VZ[n]+PZ+ZR)/distance
  79.         x = ((VX[n]+PX+XR)/z)+halfResX
  80.         y = ((VY[n]+PY+YR)/z)+halfResY
  81.         z = int(Size/z)
  82.         img.point((int(x),int(y)),0x0000ff,width=z)
  83.         record.append((x,y,z))
  84.     img.point((PX+halfResX,PY+halfResY),0xff0000,width=Size*3)
  85.     img.text((20,40),'X:'+unicode(xm),0x000000)
  86.     img.text((20,80),'Y:'+unicode(ym),0x000000)
  87.     x2,y2=xm,ym
  88.  
  89.     ANGLEX+=ym
  90.     ANGLEY+=xm
  91.     draw(())
  92.  
  93.     wait=time.time()+0.005 ### ZZZ might flicker with a less wait
  94.     while time.time() < wait:
  95.         pass
  96.     e32.ao_yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement