Advertisement
Guest User

Tesseract

a guest
Aug 14th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.02 KB | None | 0 0
  1. import pyglet
  2. from pyglet import shapes
  3. from pyglet.window import key
  4. from pyglet import clock
  5. import numpy as np
  6. import math
  7.  
  8.  
  9. def dist(c1, c2):
  10.     di = (((c1[0] - c2[0])**2)+((c1[1] - c2[1])**2)+((c1[2] - c2[2])**2)+((c1[3] - c2[3])**2))**(1/2)
  11.  
  12.     return di
  13.  
  14.  
  15. def calc_point():
  16.     pass
  17.  
  18.  
  19. def draw_lines(C, A):
  20.     lne = []
  21.     for i in range(16):
  22.         for e in range(16):
  23.             if ((dist(A[i], A[e])) != 0) and ((dist(A[i], A[e])) < 280):
  24.                 lne.append(shapes.Line(C[i][0], C[i][1], C[e][0], C[e][1], 5, color=(255, 255, 255), batch=batch))
  25.     return lne
  26.  
  27.  
  28. #########################################################################################################
  29. #########################################################################################################
  30. #########################################################################################################
  31. #########################################################################################################
  32.  
  33. angleXY = math.pi
  34. turnXY = False
  35. angleYZ = math.pi
  36. turnYZ = False
  37.  
  38. angleXZ = math.pi
  39. turnXZ = False
  40.  
  41. angleXU = math.pi
  42. turnXU = False
  43. angleYU = math.pi
  44. turnYU = False
  45. angleZU = math.pi
  46. turnZU = False
  47.  
  48. A = np.array([[-100, -100, -100, -100],
  49.               [-100, 100, -100, -100],
  50.               [100, -100, -100, -100],
  51.               [100, 100, -100, -100],
  52.               [-100, -100, 100, -100],
  53.               [-100, 100, 100, -100],
  54.               [100, -100, 100, -100],
  55.               [100, 100, 100, -100],
  56.               [-100, -100, -100, 100],
  57.               [-100, 100, -100, 100],
  58.               [100, -100, -100, 100],
  59.               [100, 100, -100, 100],
  60.               [-100, -100, 100, 100],
  61.               [-100, 100, 100, 100],
  62.               [100, -100, 100, 100],
  63.               [100, 100, 100, 100]])
  64.  
  65.  
  66. B = np.array([[0, 0],
  67.               [0, 1],
  68.               [1, 0],
  69.               [0, 0]])
  70.  
  71.  
  72.  
  73. #########################################################################################################
  74. #########################################################################################################
  75. #########################################################################################################
  76. #########################################################################################################
  77.  
  78.  
  79. # Window Draw
  80. #########################################################################################################
  81. window = pyglet.window.Window(960, 540)
  82. batch = pyglet.graphics.Batch()
  83. #########################################################################################################
  84.  
  85. @window.event
  86. def on_key_press(symbol, modifier):
  87.     global turnXY
  88.     global turnYZ
  89.     global turnXZ
  90.     global turnXU
  91.     global turnYU
  92.     global turnZU
  93.     if symbol == key.Z:
  94.         turnXY = True
  95.     if symbol == key.X:
  96.         turnYZ = True
  97.     if symbol == key.C:
  98.         turnXZ = True
  99.     if symbol == key.A:
  100.         turnXU = True
  101.     if symbol == key.S:
  102.         turnYU = True
  103.     if symbol == key.D:
  104.         turnZU = True
  105.  
  106. @window.event
  107. def on_key_release(symbol, modifier):
  108.     global turnXY
  109.     global turnYZ
  110.     global turnXZ
  111.     global turnXU
  112.     global turnYU
  113.     global turnZU
  114.     if symbol == key.Z:
  115.         turnXY = False
  116.  
  117.     if symbol == key.X:
  118.         turnYZ = False
  119.  
  120.     if symbol == key.C:
  121.         turnXZ = False
  122.  
  123.     if symbol == key.A:
  124.         turnXU = False
  125.  
  126.     if symbol == key.S:
  127.         turnYU = False
  128.  
  129.     if symbol == key.D:
  130.         turnZU = False
  131.  
  132. @window.event
  133. def on_draw():
  134.     global angleXY
  135.     global angleYZ
  136.     global angleXZ
  137.     global angleXU
  138.     global angleYU
  139.     global angleZU
  140.     if turnXY:
  141.         angleXY += math.pi * clock.tick() * 10000 / 180
  142.     if turnYZ:
  143.         angleYZ += math.pi * clock.tick() * 10000 / 180
  144.     if turnXZ:
  145.         angleXZ += math.pi * clock.tick() * 10000 / 180
  146.     if turnXU:
  147.         angleXU += math.pi * clock.tick() * 10000 / 180
  148.     if turnYU:
  149.         angleYU += math.pi * clock.tick() * 10000 / 180
  150.     if turnZU:
  151.         angleZU += math.pi * clock.tick() * 10000 / 180
  152.     window.clear()
  153.     ###################################
  154.     # Calculate points after rotating
  155.     ###################################
  156.     rotXY = np.array([[math.cos(angleXY), math.sin(angleXY), 0, 0],
  157.                       [-math.sin(angleXY), math.cos(angleXY), 0, 0],
  158.                       [0, 0, 1, 0],
  159.                       [0, 0, 0, 1]])
  160.  
  161.     rotYZ = np.array([[1, 0, 0, 0],
  162.                       [0, math.cos(angleYZ), math.sin(angleYZ), 0],
  163.                       [0, -math.sin(angleYZ), math.cos(angleYZ), 0],
  164.                       [0, 0, 0, 1]])
  165.  
  166.     rotXZ = np.array([[math.cos(angleXZ), 0, -math.sin(angleXZ), 0],
  167.                       [0, 1, 0, 0],
  168.                       [math.sin(angleXZ), 0, math.cos(angleXZ), 0],
  169.                       [0, 0, 0, 1]])
  170.  
  171.     rotXU = np.array([[math.cos(angleXU), 0, 0, math.sin(angleXU)],
  172.                       [0, 1, 0, 0],
  173.                       [0, 0, 1, 0],
  174.                       [-math.sin(angleXU), 0, 0, math.cos(angleXU)]])
  175.  
  176.     rotYU = np.array([[1, 0, 0, 0],
  177.                       [0, math.cos(angleYU), 0, -math.sin(angleYU)],
  178.                       [0, 0, 1, 0],
  179.                       [0, math.sin(angleYU), 0, math.cos(angleYU)]])
  180.  
  181.     rotZU = np.array([[1, 0, 0, 0],
  182.                       [0, 1, 0, 0],
  183.                       [0, 0, math.cos(angleZU), -math.sin(angleZU)],
  184.                       [0, 0, math.sin(angleZU), math.cos(angleZU)]])
  185.  
  186.     rt = np.dot(A, rotXY)
  187.     rt = np.dot(rt, rotYZ)
  188.     rt = np.dot(rt, rotXZ)
  189.     rt = np.dot(rt, rotXU)
  190.     rt = np.dot(rt, rotYU)
  191.     rt = np.dot(rt, rotZU)
  192.  
  193.  
  194.     ######################################
  195.     # Calculate point projection on plane
  196.     ######################################
  197.  
  198.     C = np.dot(rt, B)
  199.     for i in range (16):
  200.         C[i][0] = C[i][0] + 400
  201.         C[i][1] = C[i][1] + 265
  202.     lne = draw_lines(C, rt)
  203.     batch.draw()
  204.  
  205. pyglet.app.run()
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement