Advertisement
asweigart

Untitled

Jan 18th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1.  
  2. import bext, math, time, random
  3.  
  4. def line(x1, y1, x2, y2):
  5. """
  6. Returns a generator that produces all of the points in a line between `x1`, `y1` and `x2`, `y2`.
  7.  
  8. (Note: The `thickness` and `endcap` parameters are not yet implemented.)
  9.  
  10. >>> list(line(0, 0, 10, 3))
  11. [(0, 0), (1, 0), (2, 1), (3, 1), (4, 1), (5, 1), (6, 2), (7, 2), (8, 2), (9, 3), (10, 3)]
  12. >>> drawPoints(line(0, 0, 20, 3))
  13. OOOO,,,,,,,,,,,,,,,,,
  14. ,,,,OOOOOOO,,,,,,,,,,
  15. ,,,,,,,,,,,OOOOOO,,,,
  16. ,,,,,,,,,,,,,,,,,OOOO
  17. """
  18. x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # TODO - Do we want this line?
  19.  
  20. isSteep = abs(y2-y1) > abs(x2-x1)
  21. if isSteep:
  22. x1, y1 = y1, x1
  23. x2, y2 = y2, x2
  24. isReversed = x1 > x2
  25.  
  26. if isReversed:
  27. x1, x2 = x2, x1
  28. y1, y2 = y2, y1
  29.  
  30. deltax = x2 - x1
  31. deltay = abs(y2-y1)
  32. error = int(deltax / 2)
  33. y = y2
  34. ystep = None
  35. if y1 < y2:
  36. ystep = 1
  37. else:
  38. ystep = -1
  39. for x in range(x2, x1 - 1, -1):
  40. if isSteep:
  41. yield (y, x)
  42. else:
  43. yield (x, y)
  44. error -= deltay
  45. if error <= 0:
  46. y -= ystep
  47. error += deltax
  48. else:
  49. deltax = x2 - x1
  50. deltay = abs(y2-y1)
  51. error = int(deltax / 2)
  52. y = y1
  53. ystep = None
  54. if y1 < y2:
  55. ystep = 1
  56. else:
  57. ystep = -1
  58. for x in range(x1, x2 + 1):
  59. if isSteep:
  60. yield (y, x)
  61. else:
  62. yield (x, y)
  63. error -= deltay
  64. if error < 0:
  65. y += ystep
  66. error += deltax
  67.  
  68. def drawLine(char, x1, y1, x2, y2):
  69. for x, y in line(x1, y1, x2, y2):
  70. bext.goto(x, y)
  71. print(char, end='')
  72.  
  73.  
  74. def rotateXYZ(x, y, z, ax, ay, az):
  75. # Rotate along x
  76. rotatedX = x
  77. rotatedY = (y * math.cos(ax)) - (z * math.sin(ax))
  78. rotatedZ = (y * math.sin(ax)) + (z * math.cos(ax))
  79. x, y, z = rotatedX, rotatedY, rotatedZ
  80.  
  81. # Rotate along y
  82. rotatedX = (z * math.sin(ay)) + (x * math.cos(ay))
  83. rotatedY = y
  84. rotatedZ = (z * math.cos(ay)) - (x * math.sin(ay))
  85. x, y, z = rotatedX, rotatedY, rotatedZ
  86.  
  87. # Rotate along z
  88. rotatedX = (x * math.cos(az)) - (y * math.sin(az))
  89. rotatedY = (x * math.sin(az)) + (y * math.cos(az))
  90. rotatedZ = z
  91.  
  92. return (rotatedX, rotatedY, rotatedZ)
  93.  
  94.  
  95. def screenCoord(point1, point2, scale=10, translate=20):
  96. return (int(point1[0] * scale + translate),
  97. int(point1[1] * scale + translate),
  98. int(point2[0] * scale + translate),
  99. int(point2[1] * scale + translate))
  100.  
  101.  
  102. width, height = bext.size()
  103.  
  104. bext.fg('random')
  105.  
  106. '''
  107. Cube points:
  108. 0+-----+1
  109. / /|
  110. / / | -y
  111. 2+-----+3 | |
  112. | 4+ | +5 +-- +x
  113. | | / /
  114. | |/ +z
  115. 6+-----+7
  116. '''
  117. points = [[-1, -1, -1],
  118. [ 1, -1, -1],
  119. [-1, -1, 1],
  120. [ 1, -1, 1],
  121. [-1, 1, -1],
  122. [ 1, 1, -1],
  123. [-1, 1, 1],
  124. [ 1, 1, 1]]
  125. rotatedPoints = [None] * 10
  126. rx = ry = rz = 0
  127. step = 0
  128.  
  129. bext.clear()
  130. try:
  131. while True:
  132. # Change color
  133. if step % 15 == 0:
  134. bext.fg('random')
  135. step += 1
  136.  
  137. # Rotate cube
  138. rx += 0.05
  139. ry += 0.1
  140. rz += 0.15
  141. for i in range(len(points)):
  142. rotatedPoints[i] = rotateXYZ(*points[i], rx, ry, rz)
  143.  
  144. # Draw cube
  145. drawLine('*', *screenCoord(rotatedPoints[0], rotatedPoints[1]))
  146. drawLine('*', *screenCoord(rotatedPoints[1], rotatedPoints[3]))
  147. drawLine('*', *screenCoord(rotatedPoints[3], rotatedPoints[2]))
  148. drawLine('*', *screenCoord(rotatedPoints[2], rotatedPoints[0]))
  149.  
  150. drawLine('*', *screenCoord(rotatedPoints[0], rotatedPoints[4]))
  151. drawLine('*', *screenCoord(rotatedPoints[1], rotatedPoints[5]))
  152. drawLine('*', *screenCoord(rotatedPoints[2], rotatedPoints[6]))
  153. drawLine('*', *screenCoord(rotatedPoints[3], rotatedPoints[7]))
  154.  
  155. drawLine('*', *screenCoord(rotatedPoints[4], rotatedPoints[5]))
  156. drawLine('*', *screenCoord(rotatedPoints[5], rotatedPoints[7]))
  157. drawLine('*', *screenCoord(rotatedPoints[7], rotatedPoints[6]))
  158. drawLine('*', *screenCoord(rotatedPoints[6], rotatedPoints[4]))
  159.  
  160. time.sleep(0.05)
  161.  
  162. # Erase cube
  163. drawLine(' ', *screenCoord(rotatedPoints[0], rotatedPoints[1]))
  164. drawLine(' ', *screenCoord(rotatedPoints[1], rotatedPoints[3]))
  165. drawLine(' ', *screenCoord(rotatedPoints[3], rotatedPoints[2]))
  166. drawLine(' ', *screenCoord(rotatedPoints[2], rotatedPoints[0]))
  167.  
  168. drawLine(' ', *screenCoord(rotatedPoints[0], rotatedPoints[4]))
  169. drawLine(' ', *screenCoord(rotatedPoints[1], rotatedPoints[5]))
  170. drawLine(' ', *screenCoord(rotatedPoints[2], rotatedPoints[6]))
  171. drawLine(' ', *screenCoord(rotatedPoints[3], rotatedPoints[7]))
  172.  
  173. drawLine(' ', *screenCoord(rotatedPoints[4], rotatedPoints[5]))
  174. drawLine(' ', *screenCoord(rotatedPoints[5], rotatedPoints[7]))
  175. drawLine(' ', *screenCoord(rotatedPoints[7], rotatedPoints[6]))
  176. drawLine(' ', *screenCoord(rotatedPoints[6], rotatedPoints[4]))
  177.  
  178.  
  179. except KeyboardInterrupt:
  180. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement