Guest User

Untitled

a guest
Sep 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. from scene import *
  2. #import sound
  3. import random
  4. import math
  5. import numpy as np
  6. A = Action
  7.  
  8.  
  9. class Shape3D (object):
  10. '''numpy version of original Vector3d code'''
  11. PROJ=np.mat(np.eye(3)[0:2])
  12. def __init__(self, values=[[]],conn=[[]], r=10):
  13. self.r=r
  14. self.vector = np.matrix(values)
  15. self.conn=conn #connection matrix. [0,1],[1,2], etc
  16. self.initvector=self.vector
  17. def reset(self):
  18. self.vector=self.initvector
  19. def as_path(self):
  20. '''return a ui.Path'''
  21. r=self.r
  22. conn=self.conn
  23. p=ui.Path()
  24. pts=self.to2D().T
  25. for c in self.conn:
  26. p.move_to(*pts[c[0],:].tolist()[0])
  27. p.line_to(*pts[c[1],:].tolist()[0])
  28. for pt in pts:
  29. p.append_path( p.oval(pt[0,0]-r, pt[0,1]-r,r*2,r*2) )
  30. #debug: show border
  31. #p.append_path(p.rect(*p.bounds.as_tuple()))
  32. return p
  33. def to2D(self):
  34. return Shape3D.PROJ*self.vector
  35.  
  36. def rotateX(self, fi):
  37. c=math.cos(fi)
  38. s=math.sin(fi)
  39. rotMat = np.matrix([[c, -s, 0],
  40. [s, c, 0],
  41. [0, 0, 1]])
  42. self.rotate(rotMat)
  43.  
  44. def rotateY(self, fi):
  45. c=math.cos(fi)
  46. s=math.sin(fi)
  47. rotMat = np.matrix([[c, 0, -s],
  48. [0, 1, 0],
  49. [s, 0, c]])
  50. self.rotate(rotMat)
  51.  
  52. def rotateZ(self, fi):
  53. c=math.cos(fi)
  54. s=math.sin(fi)
  55. rotMat = np.matrix([[1, 0, 0],
  56. [0, c, -s],
  57. [0, s, c]])
  58. self.rotate(rotMat)
  59.  
  60. def rotate(self, rotMat):
  61. self.vector = rotMat*self.vector
  62.  
  63.  
  64.  
  65. Vector3D=lambda x:np.matrix(x).T
  66. class MyScene (Scene):
  67. def setup(self):
  68. self.angle = 0.005
  69. self.angleX = 0
  70. self.angleY = 0
  71. self.init_angle = self.angle
  72. self.centro = np.mat([500,400,0]).T
  73. #----CUBE-----
  74. self.p = np.mat([[-100,-100,-100],
  75. [100,-100,-100],
  76. [100,100,-100],
  77. [-100,100,-100],
  78. [-100,-100,100],
  79. [100,-100,100],
  80. [100,100,100],
  81. [-100,100,100]]).T
  82. #pdist=np.power(sum([np.power((s.p[i0]-s.p[i0].T),2) for i0 in range(3)]),.5)
  83. conn=[[0,1],
  84. [0,3],
  85. [0,4],
  86. [1,2],
  87. [1,5],
  88. [2,3],
  89. [2,6],
  90. [3,7],
  91. [4,5],
  92. [4,7],
  93. [5,6],
  94. [6,7] ]
  95.  
  96.  
  97. #-----PIRAMID-----
  98. '''
  99. self.p = np.mat([[-100,-100,-100],
  100. [100,-100,-100],
  101. [100,100,-100],
  102. [-100,100,-100],
  103. [0,0,100]]).T
  104. conn=[[0,1],
  105. [0,3],
  106. [0,4],
  107. [1,2],
  108. [1,4],
  109. [2,3],
  110. [2,4],
  111. [3,4]]
  112. '''
  113.  
  114. self.shape=Shape3D(self.p,conn)
  115. self.puntos = []
  116.  
  117. self.shapenode=ShapeNode()
  118. self.shapenode.position=[500,400]
  119. self.shapenode.stroke_color = '#ffff00'
  120. self.shapenode.color='#ff00ff'
  121. #self.shapenode.fill_color='#0000ff'
  122.  
  123. self.add_child(self.shapenode)
  124. self.update_pts()
  125.  
  126. def did_change_size(self):
  127. self.update_pts()
  128. pass
  129.  
  130. def update_pts(self):
  131. self.shape.rotateY(self.angleX)
  132. self.shape.rotateZ(self.angleY)
  133. self.shapenode.path=self.shape.as_path()
  134.  
  135.  
  136. def touch_began(self, touch):
  137. #self.angle = 0
  138. self.move = 0
  139. x, y = touch.location
  140. self.inicio = {'x':x, 'y':y}
  141.  
  142. def touch_moved(self, touch):
  143. self.move = 1
  144. x, y = touch.location
  145. x = abs(self.inicio['x']) - abs(x)
  146. y = abs(self.inicio['y']) - abs(y)
  147. self.angleX = math.radians(x)/50
  148. self.angleY = math.radians(y)/50
  149. self.update_pts()
  150.  
  151. def touch_ended(self, touch):
  152. #self.angle = self.init_angle
  153. if self.move == 1:
  154. self.angleX = 0
  155. self.angleY = 0
  156. else:
  157. self.angleX = self.angle
  158. self.angleY = self.angle
  159. self.update_pts()
  160.  
  161. if __name__ == '__main__':
  162. s=MyScene()
  163. run(s, show_fps=True)
Add Comment
Please, Sign In to add comment