Advertisement
Guest User

Untitled

a guest
May 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. import math
  2. import numpy as np
  3. from DisplayProxy import DisplayProxy
  4. import time
  5. from colorsys import hsv_to_rgb
  6.  
  7. CUBE_SIZE = 8
  8. COLS = 20
  9. ROWS = 12
  10.  
  11. def get_4x4_transform(trans_x, trans_y, trans_z):
  12. transform = [[1, 0, 0, trans_x],
  13. [0, 1, 0, trans_y],
  14. [0, 0, 1, trans_z],
  15. [0, 0, 0, 1]]
  16. return np.matrix(transform)
  17.  
  18. xAngle = 0
  19. yAngle = 0
  20. zAngle = 0
  21. def update_angle(i):
  22. global xAngle
  23. global yAngle
  24. global zAngle
  25. xAngle += 0.1
  26. yAngle += 0.1
  27. zAngle = 0
  28.  
  29. rotX = np.matrix([
  30. [1, 0, 0, 0],
  31. [0, math.cos(xAngle), -math.sin(xAngle), 0],
  32. [0, math.sin(xAngle), math.cos(xAngle), 0],
  33. [0, 0, 0, 1]
  34. ])
  35.  
  36. rotY = np.matrix([
  37. [math.cos(yAngle), 0, math.sin(yAngle), 0],
  38. [0, 1, 0, 0],
  39. [-math.sin(yAngle), 0, math.cos(yAngle), 0],
  40. [0, 0, 0, 1]
  41. ])
  42.  
  43. rotZ = np.matrix([
  44. [math.cos(zAngle), -math.sin(zAngle), 0, 0],
  45. [math.sin(zAngle), math.cos(zAngle), 0, 0],
  46. [0, 0, 1, 0],
  47. [0, 0, 0, 1]
  48. ])
  49.  
  50. return rotX, rotY, rotZ
  51.  
  52. def inside_polygon(x, y, points):
  53. """
  54. Return True if a coordinate (x, y) is inside a polygon defined by
  55. a list of verticies [(x1, y1), (x2, x2), ... , (xN, yN)].
  56.  
  57. Reference: http://www.ariel.com.au/a/python-point-int-poly.html
  58. """
  59. n = len(points)
  60. inside = False
  61. p1x, p1y = points[0]
  62. for i in range(1, n + 1):
  63. p2x, p2y = points[i % n]
  64. if y > min(p1y, p2y):
  65. if y <= max(p1y, p2y):
  66. if x <= max(p1x, p2x):
  67. if p1y != p2y:
  68. xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
  69. if p1x == p2x or x <= xinters:
  70. inside = not inside
  71. p1x, p1y = p2x, p2y
  72. return inside
  73.  
  74. cube_vertices = np.matrix([
  75. # 0 bottom left back
  76. [-CUBE_SIZE/2, -CUBE_SIZE/2, -CUBE_SIZE/2, 1],
  77. # 1 bottom right back
  78. [CUBE_SIZE/2, -CUBE_SIZE/2, -CUBE_SIZE/2, 1],
  79. # 2 top left back
  80. [-CUBE_SIZE/2, CUBE_SIZE/2, -CUBE_SIZE/2, 1],
  81. # 3 top right back
  82. [CUBE_SIZE/2, CUBE_SIZE/2, -CUBE_SIZE/2, 1],
  83. # 4 bottom left front
  84. [-CUBE_SIZE/2, -CUBE_SIZE/2, CUBE_SIZE/2, 1],
  85. # 5 bottom right front
  86. [CUBE_SIZE/2, -CUBE_SIZE/2, CUBE_SIZE/2, 1],
  87. # 6 top left front
  88. [-CUBE_SIZE/2, CUBE_SIZE/2, CUBE_SIZE/2, 1],
  89. # 7 top right front
  90. [CUBE_SIZE/2, CUBE_SIZE/2, CUBE_SIZE/2, 1],
  91. ])
  92.  
  93. sides = {'back': [0, 1, 3, 2], 'left': [0, 2, 6, 4], 'right': [1, 3, 7, 5], 'front': [4, 5, 7, 6],
  94. 'bottom': [0, 1, 5, 4], 'top': [6, 7, 3, 2]}
  95.  
  96.  
  97. side_colors = {
  98. 'left': (244, 100, 100),
  99. 'back': (75,75,75),
  100. 'front': (144,144,120),
  101. 'right': (0,0,125),
  102. 'top': (21,144,2),
  103. 'bottom': (12,211,144),
  104. }
  105.  
  106. def distance(d1, d2):
  107. if len(d2) >= 3 and len(d1) >= 3:
  108. return math.sqrt((d1[0]-d2[0])**2+(d1[1]-d2[1])**2+(d1[2]-d2[2])**2)
  109. return math.sqrt((d1[0]-d2[0])**2+(d1[1]-d2[1])**2)
  110.  
  111.  
  112. def get_color(side, i, coord, points):
  113. d = min([distance(points[s][:2], coord) for s in sides[side]]) / CUBE_SIZE
  114. #if side == 'left':
  115. #hue = ((math.sin(i/9) + 1)/2)
  116. #return [x*255 + 140 * (0.3 - d) for x in hsv_to_rgb(0.2, 1, 1)]
  117. #return [x*255 * d for x in hsv_to_rgb(d-0.25, 1, 1)]
  118. #if side == 'right':
  119. #x = [p[0] for p in points]
  120. # = [p[1] for p in points]
  121. #centroid = (sum(x) / len(points), sum(y) / len(points))
  122. #centroid = (sum(x) / len(points), sum(y) / len(points))
  123. #return [x*255 * d for x in hsv_to_rgb(d, 1, 1)]
  124. return [-50 + x - 300 * (0.2 - d) for x in side_colors[side]]
  125.  
  126.  
  127. def background_plasma(i):
  128. w = COLS
  129. h = ROWS
  130. i /= 5
  131. for x in range (COLS):
  132. for y in range(ROWS):
  133. hue = math.sin(x/10 + math.sin(i/10)*10 + i/40) + 1
  134. hue += math.cos(y/10 + i/3) + 1
  135. hue /= 4.0
  136. hsv = hsv_to_rgb(hue, 1.0, 1.0)
  137. col = tuple([int(round(c * 255.0)) for c in hsv])
  138. display.set_pixel((x, y), [x * 0.02 for x in col])
  139.  
  140.  
  141. points = []
  142. i = 0
  143. display = DisplayProxy()
  144. while True:
  145. highest = [[0] * COLS for _ in range(ROWS)]
  146. display.set_fill((10,10,10))
  147. background_plasma(i)
  148. i+=1;
  149. rotX, rotY, rotZ = update_angle(i)
  150. trans = get_4x4_transform(9 + math.sin(i/10)*5, 6 + math.cos(i/10)*2, 10)
  151. coords = []
  152. points = []
  153. dist = [[0] * COLS for _ in range(ROWS)]
  154. for x in cube_vertices:
  155. x = x.dot(rotX)
  156. x = x.dot(rotY)
  157. x = trans.dot(x.T)
  158. (x, y, z, w) = x.T.tolist()[0]
  159. points.append((x, y, z, w))
  160. coords.append((int(x), int(y)))
  161. highest = [[0] * COLS for _ in range(ROWS)]
  162. for y in range(ROWS):
  163. for x in range(COLS):
  164. high_n = -1
  165. color = None
  166. for k, side in sides.items():
  167. stuff = [points[s][:2] for s in side]
  168. if inside_polygon(x, y, stuff):
  169. for p in side:
  170. if points[p][2] > high_n:
  171. high_n = points[p][2]
  172. color = k
  173. highest[y][x] = {'n': high_n, 'color': color}
  174. if color:
  175. display.set_pixel((x, y), get_color(color, i, (x,y, high_n), points))
  176.  
  177. for side in sides:
  178.  
  179. pass
  180.  
  181.  
  182. display.show()
  183. time.sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement