Advertisement
aaaranes

Draft for Arts 1

Apr 21st, 2024 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | Source Code | 0 0
  1. # Framework to use to have a window for showing the figure
  2. import pygame
  3. import math
  4.  
  5. # WINDOWS SIZE PARAMETERS
  6. WIDTH = 800
  7. HEIGHT = 600
  8. SCALE = 100
  9.  
  10. angle = 0
  11.  
  12. # CONSTANTS
  13. WHITE = (255,255,255)
  14. RED = (255,0,0)
  15. GREEN = (0,255,0)
  16. BLUE = (0,0,255)
  17. BLACK = (0,0,0)
  18.  
  19. SCREEN_CENTER = (WIDTH/2,HEIGHT/2)
  20.  
  21. # Matrix Operation Functions
  22. def matMult(mat1, mat2):
  23.     mat3 = []
  24.     if (len(mat1[0]) == len(mat2)):
  25.         for i in range(len(mat1)):
  26.             temp = []
  27.             total = 0
  28.             for j in range(len(mat1[0])):
  29.                 total += mat2[j] * mat1[i][j]
  30.  
  31.             temp.append(total)
  32.             mat3.append(temp)
  33.     else:
  34.         print("Invalid Matrix size")
  35.         return None
  36.     return mat3
  37.  
  38. # Figure vertices
  39. cube = [
  40.     [-1,1,-1],
  41.     [1,1,-1],
  42.     [1,-1,-1],
  43.     [-1,-1,-1],
  44.     [1,1,1],
  45.     [-1,1,1],
  46.     [-1,-1,1],
  47.     [1,-1,1]
  48. ]
  49.  
  50. # Projection Matrix
  51. projectionMat = [
  52.     [1,0,0],
  53.     [0,1,0],
  54.     [0,0,0]
  55. ]
  56.  
  57.  
  58.  
  59. # Setup pygame
  60. pygame.init()
  61. screen = pygame.display.set_mode((WIDTH,HEIGHT))
  62. clock = pygame.time.Clock()
  63. running = True
  64.  
  65. # Main program loop
  66. while running:
  67.     # Events
  68.     for event in pygame.event.get():
  69.         if event.type == pygame.QUIT:
  70.             running = False
  71.         if (event.type == pygame.KEYDOWN) and (event.key == pygame.K_ESCAPE):
  72.             running = False
  73.    
  74.     screen.fill(WHITE)
  75.  
  76.     # Render the cube vertices
  77.     # X-axis Matrix Rotation
  78.     rotateX = [
  79.         [1,0,0],
  80.         [0, math.cos(angle), -math.sin(angle)],
  81.         [0, math.sin(angle), math.cos(angle)]
  82.     ]
  83.  
  84.     # Y-axis Matrix Rotation
  85.     rotateY = [
  86.         [math.cos(angle), 0, math.sin(angle)],
  87.         [0, 1, 0],
  88.         [-math.sin(angle), 0, math.cos(angle)]
  89.     ]
  90.  
  91.     # Z-axis Matrix Rotation
  92.     rotateZ = [
  93.         [math.cos(angle), -math.sin(angle),0],
  94.         [math.sin(angle), math.cos(angle), 0],
  95.         [0,0,1]
  96.     ]
  97.  
  98.  
  99.     angle += 0.01
  100.     for i in range(8):
  101.             projectedPoint = matMult(projectionMat,cube[i])
  102.             if i<4:
  103.                 x = projectedPoint[0][0]*SCALE + SCREEN_CENTER[0]
  104.                 y = projectedPoint[1][0]*SCALE + SCREEN_CENTER[1]
  105.             else:
  106.                 x = projectedPoint[0][0]*SCALE + SCREEN_CENTER[0] - SCALE/2
  107.                 y = projectedPoint[1][0]*SCALE + SCREEN_CENTER[1] - SCALE/2              
  108.             pygame.draw.circle(screen, RED, (x,y), 5)
  109.             pygame.draw.circle(screen, RED, (x,y), 5)
  110.  
  111.     # Render the cube edges
  112.  
  113.     # Show changes
  114.     pygame.display.flip()
  115.     clock.tick(60)
  116.  
  117.  
  118.  
  119. pygame.quit()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement