thauweer

cardioid.py

Apr 5th, 2023
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. from numpy import pi, sin, cos
  2. import pygame
  3. from time import sleep
  4.  
  5.  
  6. # colors
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9. GREEN = (0, 255, 0)
  10. BLUE = (0, 0, 255)
  11.  
  12. pygame.init()
  13. canvas = pygame.display.set_mode((800, 800))
  14. canvas.fill(BLACK)
  15. pygame.display.set_caption("trygonometry test")
  16. exit = False
  17.  
  18. center = x, y = [400, 400]
  19. iterations = 360
  20. radius = 400
  21.  
  22. points = []
  23.  
  24.  
  25.  
  26. for j in range(iterations):
  27.     step_x = sin(2*pi/iterations * j) * radius + x
  28.     step_y = cos(2*pi/iterations * j) * radius + y
  29.     points.append([step_x, step_y])
  30.  
  31. i = 0
  32.  
  33. step = 2
  34.  
  35. while not exit:
  36.     for event in pygame.event.get():
  37.         if event.type == pygame.QUIT:
  38.             exit = True
  39.         if event.type == pygame.KEYDOWN:
  40.             if event.key == pygame.K_r:
  41.                 canvas.fill(BLACK)
  42.                 step=3
  43.             if event.unicode == "+":
  44.                 canvas.fill(BLACK)
  45.                 step += 1
  46.             if event.unicode == "-":
  47.                 canvas.fill(BLACK)
  48.                 step -= 1
  49.    
  50.     pygame.draw.circle(canvas, WHITE, center, radius, 1)
  51.        
  52.     pygame.draw.line(canvas, BLUE, points[i], points[i*step % iterations])
  53.    
  54.     for j, point in enumerate(points):
  55.         pygame.draw.circle(canvas, GREEN, point, 2)
  56.     #     pygame.draw.line(canvas, BLUE, point, points[j*6 % iterations])
  57.    
  58.     i += 1
  59.     i = 0 if i == iterations else i
  60.     pygame.display.update()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment