Advertisement
Guest User

Spinning Arcs

a guest
Feb 2nd, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from OpenGL.GL import *
  4. from OpenGL.GLU import *
  5. import math
  6.  
  7. # Initialize Pygame and set OpenGL attributes for multisampling
  8. pygame.init()
  9. pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
  10. # Increase multisample samples for improved anti-aliasing
  11. pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 16)
  12.  
  13. width, height = 800, 600
  14. screen = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)
  15. pygame.display.set_caption("Extra Smooth OpenGL Spinning Circles with Zoom")
  16.  
  17. # Setup a 2D orthographic projection
  18. glViewport(0, 0, width, height)
  19. glMatrixMode(GL_PROJECTION)
  20. glLoadIdentity()
  21. gluOrtho2D(0, width, 0, height)
  22. glMatrixMode(GL_MODELVIEW)
  23. glLoadIdentity()
  24.  
  25. # Enable anti-aliasing features
  26. glEnable(GL_BLEND)
  27. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  28. glEnable(GL_LINE_SMOOTH)
  29. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
  30. glEnable(GL_MULTISAMPLE)
  31.  
  32. # Parameters for the arcs
  33. center = (width / 2, height / 2)
  34. base_radius = 40
  35. spacing = 20
  36. num_circles = 10
  37. rotation_angles = [0] * num_circles
  38. base_rotation_speed = 1.0  # degrees per frame
  39. gap_angle = 25            # gap in degrees
  40. line_color = (0, 255, 255)  # Cyan
  41. line_width = 3
  42. clockwise = False  # True means rotating clockwise
  43.  
  44. # Radii and rotation speeds for each circle
  45. radii = [base_radius + i * spacing for i in range(num_circles)]
  46. rotation_speeds = [base_rotation_speed / (1 + i * 0.3) for i in range(num_circles)]
  47.  
  48. # Zoom factor (1.0 means no zoom)
  49. zoom = 1.0
  50.  
  51. def draw_arc(cx, cy, radius, start_angle, end_angle, segments):
  52.     """
  53.    Draw an arc using GL_LINE_STRIP from start_angle to end_angle (in radians)
  54.    with a specified number of segments.
  55.    """
  56.     glBegin(GL_LINE_STRIP)
  57.     for i in range(segments + 1):
  58.         t = start_angle + (end_angle - start_angle) * i / segments
  59.         x = cx + radius * math.cos(t)
  60.         y = cy + radius * math.sin(t)
  61.         glVertex2f(x, y)
  62.     glEnd()
  63.  
  64. clock = pygame.time.Clock()
  65. running = True
  66.  
  67. while running:
  68.     for event in pygame.event.get():
  69.         if event.type == QUIT:
  70.             running = False
  71.         # Use the mouse wheel for zooming
  72.         elif event.type == pygame.MOUSEBUTTONDOWN:
  73.             if event.button == 4:  # Scroll up -> zoom in
  74.                 zoom *= 1.1
  75.             elif event.button == 5:  # Scroll down -> zoom out
  76.                 zoom /= 1.1
  77.         # Use the keyboard for zooming
  78.         elif event.type == pygame.KEYDOWN:
  79.             if event.key in (pygame.K_PLUS, pygame.K_KP_PLUS, pygame.K_EQUALS):
  80.                 zoom *= 1.1
  81.             elif event.key in (pygame.K_MINUS, pygame.K_KP_MINUS):
  82.                 zoom /= 1.1
  83.  
  84.     # Update rotation angles; subtract to rotate clockwise
  85.     for i in range(num_circles):
  86.         if clockwise:
  87.             rotation_angles[i] = (rotation_angles[i] - rotation_speeds[i]) % 360
  88.         else:
  89.             rotation_angles[i] = (rotation_angles[i] + rotation_speeds[i]) % 360
  90.  
  91.     # Clear the screen with a black background
  92.     glClearColor(0, 0, 0, 1)
  93.     glClear(GL_COLOR_BUFFER_BIT)
  94.  
  95.     # Apply the zoom transformation:
  96.     #   1. Reset the modelview matrix.
  97.     #   2. Translate to the center.
  98.     #   3. Scale by the zoom factor.
  99.     #   4. Translate back.
  100.     glMatrixMode(GL_MODELVIEW)
  101.     glLoadIdentity()
  102.     glTranslatef(center[0], center[1], 0)
  103.     glScalef(zoom, zoom, 1)
  104.     glTranslatef(-center[0], -center[1], 0)
  105.  
  106.     # Set line properties
  107.     glLineWidth(line_width)
  108.     glColor3f(line_color[0] / 255.0,
  109.               line_color[1] / 255.0,
  110.               line_color[2] / 255.0)
  111.  
  112.     # Draw each arc (circle with a gap)
  113.     for radius, angle in zip(radii, rotation_angles):
  114.         start_angle = math.radians(angle)
  115.         arc_span = 2 * math.pi - math.radians(gap_angle)
  116.         end_angle = start_angle + arc_span
  117.         # More segments produce a smoother arc
  118.         segments = max(50, int((arc_span * radius) / 2))
  119.         draw_arc(center[0], center[1], radius, start_angle, end_angle, segments)
  120.  
  121.     pygame.display.flip()
  122.     clock.tick(60)
  123.  
  124. pygame.quit()
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement