Advertisement
here2share

# Tk_CircleVsCircle_Collision.py

May 13th, 2020
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. # Tk_CircleVsCircle_Collision.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk, ImageDraw
  5. import random
  6. import math
  7.  
  8. imgx = 800
  9. imgy = 600
  10. root = Tk()
  11. root.title("Tk CircleVsCircle Collision")
  12. root.geometry("%dx%d+0+0"%(imgx,imgy))
  13. canvas = Canvas(root, width=imgx, height=imgy)
  14. canvas.grid()
  15. pic = Image.new("RGB", (imgx, imgy))
  16. draw = ImageDraw.Draw(pic)
  17.  
  18. n = 12
  19. p = 20
  20. crMax = int(min(imgx - 1, imgy - 1) / 4) # max circle radius
  21. crMin = 40 # min circle radius
  22.  
  23. # create circular obstacle(s)
  24. cxList = []
  25. cyList = []
  26. crList = []
  27. for i in range(n):
  28.     while(True): # circle(s) must not overlap
  29.         cr = random.randint(crMin, crMax) # circle radius
  30.         cx = random.randint(cr + p, imgx - p - cr) # circle center x
  31.         cy = random.randint(cr + p, imgy - p - cr) # circle center y
  32.         flag = True
  33.         if i > 0:
  34.             for j in range(i):
  35.                 if math.hypot(cx - cxList[j], cy - cyList[j]) < cr + crList[j] + 30:
  36.                     flag = False
  37.                     break
  38.         if flag == True:
  39.             break
  40.     draw.ellipse((cx - cr, cy - cr, cx + cr, cy + cr), fill='green')
  41.     cxList.append(cx)
  42.     cyList.append(cy)
  43.     crList.append(cr)
  44.  
  45. # initial location of the ball must be outside of the circle(s)
  46. while(True):
  47.     x = float(random.randint(0, imgx - 1))
  48.     y = float(random.randint(0, imgy - 1))
  49.     flag = False
  50.     for i in range(n):
  51.         if math.hypot(x - cxList[i], y - cyList[i]) <= crList[i]:
  52.             flag = True
  53.             break
  54.     if flag == False:
  55.         break
  56.    
  57. # initial direction of the ball
  58. a = 2.0 * math.pi * random.random()
  59. s = math.sin(a)
  60. c = math.cos(a)
  61.  
  62. while 1:
  63.     pic.putpixel((int(x), int(y)), (255, 255, 255))
  64.     xnew = x + c
  65.     ynew = y + s
  66.  
  67.     # reflection from the walls
  68.     if xnew < 0 or xnew > imgx - 1:
  69.         c = -c
  70.         xnew = x
  71.     if ynew < 0 or ynew > imgy - 1:
  72.         s = -s
  73.         ynew = y
  74.  
  75.     # reflection from the circle(s)
  76.     for i in range(n):
  77.         if math.hypot(xnew - cxList[i], ynew - cyList[i]) <= crList[i]:
  78.             # angle of the circle point
  79.             ca = math.atan2(ynew - cyList[i], xnew - cxList[i])
  80.             # reversed collision angle of the ball
  81.             rca = math.atan2(-s, -c)
  82.             # reflection angle of the ball
  83.             rab = rca + (ca - rca) * 2
  84.             s = math.sin(rab)
  85.             c = math.cos(rab)
  86.             xnew = x
  87.             ynew = y
  88.  
  89.     x = xnew
  90.     y = ynew
  91.     imgTk = ImageTk.PhotoImage(pic)
  92.     canvas.create_image(0, 0, anchor=NW, image=imgTk)
  93.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement