Guest User

Qwen test

a guest
Jul 30th, 2025
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.10 KB | Gaming | 0 0
  1. import math
  2. import tkinter as tk
  3. import time
  4.  
  5. class Ball:
  6.     def __init__(self, x, y, vx, vy, radius, color, number, angular_velocity):
  7.         self.x = x
  8.         self.y = y
  9.         self.vx = vx
  10.         self.vy = vy
  11.         self.radius = radius
  12.         self.color = color
  13.         self.number = number
  14.         self.angular_velocity = angular_velocity
  15.         self.theta = 0.0
  16.  
  17. def closest_point_on_segment(px, py, x1, y1, x2, y2):
  18.     vx = x2 - x1
  19.     vy = y2 - y1
  20.     wx = px - x1
  21.     wy = py - y1
  22.     dot = vx * wx + vy * wy
  23.     len2 = vx * vx + vy * vy
  24.     if len2 == 0:
  25.         return (x1, y1), math.sqrt(wx * wx + wy * wy)
  26.     t = max(0.0, min(1.0, dot / len2))
  27.     cx = x1 + t * vx
  28.     cy = y1 + t * vy
  29.     dx = px - cx
  30.     dy = py - cy
  31.     d = math.sqrt(dx * dx + dy * dy)
  32.     return (cx, cy), d
  33.  
  34. def main():
  35.     R = 300
  36.     num_sides = 7
  37.     vertices = []
  38.     for i in range(num_sides):
  39.         angle = i * 2 * math.pi / num_sides
  40.         x = R * math.cos(angle)
  41.         y = R * math.sin(angle)
  42.         vertices.append((x, y))
  43.    
  44.     midpoints = []
  45.     for i in range(num_sides):
  46.         mid_x = (vertices[i][0] + vertices[(i + 1) % num_sides][0]) / 2
  47.         mid_y = (vertices[i][1] + vertices[(i + 1) % num_sides][1]) / 2
  48.         midpoints.append((mid_x, mid_y))
  49.    
  50.     normals = []
  51.     for mid in midpoints:
  52.         mag = math.sqrt(mid[0] * mid[0] + mid[1] * mid[1])
  53.         nx = mid[0] / mag
  54.         ny = mid[1] / mag
  55.         normals.append((nx, ny))
  56.    
  57.     colors = [
  58.         "#f8b862", "#f6ad49", "#f39800", "#f08300", "#ec6d51", "#ee7948", "#ed6d3d",
  59.         "#ec6800", "#ec6800", "#ee7800", "#eb6238", "#ea5506", "#ea5506", "#eb6101",
  60.         "#e49e61", "#e45e32", "#e17b34", "#dd7a56", "#db8449", "#d66a35"
  61.     ]
  62.    
  63.     balls = []
  64.     ball_radius = 15
  65.     # Slightly offset initial positions in a small circle
  66.     for i in range(20):
  67.         angle = i * 2 * math.pi / 20
  68.         offset_x = 0.5 * math.cos(angle)
  69.         offset_y = 0.5 * math.sin(angle)
  70.         angular_velocity = 0.1
  71.         balls.append(Ball(offset_x, offset_y, 0.0, -0.1, ball_radius, colors[i], i + 1, angular_velocity))
  72.    
  73.     root = tk.Tk()
  74.     root.title("Bouncing Balls in Rotating Heptagon")
  75.     canvas = tk.Canvas(root, width=800, height=800, bg='white')
  76.     canvas.pack()
  77.    
  78.     dt = 0.01
  79.     angular_velocity_heptagon = 2 * math.pi / 5.0
  80.     start_time = time.time()
  81.    
  82.     def draw_heptagon():
  83.         current_time = time.time()
  84.         elapsed = current_time - start_time
  85.         angle = (angular_velocity_heptagon * elapsed) % (2 * math.pi)
  86.         rotated_vertices = []
  87.         for (x, y) in vertices:
  88.             new_x = x * math.cos(angle) - y * math.sin(angle)
  89.             new_y = x * math.sin(angle) + y * math.cos(angle)
  90.             rotated_vertices.append((new_x, new_y))
  91.        
  92.         points = []
  93.         for (x, y) in rotated_vertices:
  94.             points.append(x + 400)
  95.             points.append(y + 400)
  96.         canvas.create_polygon(points, outline='black', fill='', width=2)
  97.    
  98.     def draw_ball(ball):
  99.         x = ball.x + 400
  100.         y = ball.y + 400
  101.         canvas.create_oval(x - ball.radius, y - ball.radius, x + ball.radius, y + ball.radius, fill=ball.color, outline='black')
  102.         canvas.create_text(x, y, text=str(ball.number), font=('Arial', 12, 'bold'), fill='black')
  103.    
  104.     while True:
  105.         current_time = time.time()
  106.         elapsed = current_time - start_time
  107.         angle = (angular_velocity_heptagon * elapsed) % (2 * math.pi)
  108.        
  109.         for ball in balls:
  110.             ball.vy += 100.0 * dt
  111.             ball.x += ball.vx * dt
  112.             ball.y += ball.vy * dt
  113.             ball.angular_velocity *= (1.0 - 0.01 * dt)
  114.             ball.theta += ball.angular_velocity * dt
  115.        
  116.         for ball in balls:
  117.             for edge_index in range(num_sides):
  118.                 v1 = vertices[edge_index]
  119.                 v2 = vertices[(edge_index + 1) % num_sides]
  120.                 closest, d = closest_point_on_segment(ball.x, ball.y, v1[0], v1[1], v2[0], v2[1])
  121.                 if d < ball.radius:
  122.                     nx, ny = normals[edge_index]
  123.                     normal = (-nx, -ny)
  124.                     relative_vel = ball.vx * normal[0] + ball.vy * normal[1]
  125.                     if relative_vel > 0:
  126.                         continue
  127.                     restitution = 0.1
  128.                     j = -(1 + restitution) * relative_vel
  129.                     ball.vx += j * normal[0]
  130.                     ball.vy += j * normal[1]
  131.                     penetration = ball.radius - d
  132.                     ball.x += penetration * normal[0]
  133.                     ball.y += penetration * normal[1]
  134.        
  135.         for i in range(len(balls)):
  136.             for j in range(i + 1, len(balls)):
  137.                 ball1 = balls[i]
  138.                 ball2 = balls[j]
  139.                 dx = ball2.x - ball1.x
  140.                 dy = ball2.y - ball1.y
  141.                 dist = math.sqrt(dx * dx + dy * dy)
  142.                 if dist < ball1.radius + ball2.radius:
  143.                     if dist < 1e-5:  # Prevent division by zero
  144.                         dist = 1e-5
  145.                     nx = dx / dist
  146.                     ny = dy / dist
  147.                     dvx = ball2.vx - ball1.vx
  148.                     dvy = ball2.vy - ball1.vy
  149.                     dv_normal = dvx * nx + dvy * ny
  150.                     if dv_normal < 0:
  151.                         restitution = 0.2
  152.                         j = -(1 + restitution) * dv_normal
  153.                         ball1.vx -= j * nx
  154.                         ball1.vy -= j * ny
  155.                         ball2.vx += j * nx
  156.                         ball2.vy += j * ny
  157.                     overlap = ball1.radius + ball2.radius - dist
  158.                     ball1.x -= overlap * nx * 0.5
  159.                     ball1.y -= overlap * ny * 0.5
  160.                     ball2.x += overlap * nx * 0.5
  161.                     ball2.y += overlap * ny * 0.5
  162.        
  163.         canvas.delete("all")
  164.         draw_heptagon()
  165.         for ball in balls:
  166.             draw_ball(ball)
  167.        
  168.         root.update()
  169.         time.sleep(0.001)
  170.  
  171. if __name__ == "__main__":
  172.     main()
  173.  
Tags: Qwen test
Advertisement
Add Comment
Please, Sign In to add comment