Advertisement
cookertron

Add points to a Quadtree - Python Pygame

May 20th, 2020
1,636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import pygame
  2. from pygame import gfxdraw
  3. import random
  4.  
  5. # quadTree constants
  6. CAPACITY = 4
  7.  
  8. # This breaks down the display area into populated rectangles. The population is based on points
  9. class quadTree:
  10.     def __init__(s, rect): # boundry is a rectangle
  11.         s.rect = rect
  12.         s.points = []
  13.         s.divided = False
  14.        
  15.     def insert(s, point):
  16.         global PD # Primary Display
  17.         global CAPACITY # maximum capacity on each quadrant
  18.  
  19.         # if the point inserted does fit into this quadrant then don't add it to
  20.         # the points list. Exit insert()!
  21.         if not s.rect.collidepoint(int(point.x), int(point.y)): return False
  22.  
  23.         # draw the point to the Primary Display
  24.         pygame.draw.circle(PD, (255, 0, 0), (int(point.x), int(point.y)), 3)
  25.  
  26.         # check if the quadrant has room for more points
  27.         if len(s.points) < CAPACITY:
  28.             s.points.append(point) # if there's room add it to the points list
  29.             # return True to inform parent quadtree the point has been added
  30.             return True
  31.         else:
  32.             # if the quadrant is full then divide the quadrant into
  33.             # four parts north west, north east, south west, south east...
  34.             if not s.divided:
  35.                 s.subDivide()
  36.        
  37.             # ...and insert the point into one of the four new quadrants
  38.             if s.nw.insert(point):
  39.                 return True
  40.             elif s.ne.insert(point):
  41.                 return True
  42.             elif s.sw.insert(point):
  43.                 return True
  44.             elif s.se.insert(point):
  45.                 return True
  46.  
  47.     def subDivide(s):
  48.         global PD # Primary Display
  49.  
  50.         # create four new quadtrees which belong to the parent quadtree
  51.         size = (s.rect.width / 2, s.rect.height / 2)
  52.         s.nw = quadTree(pygame.Rect(s.rect.topleft, size))
  53.         s.ne = quadTree(pygame.Rect(s.rect.midtop, size))
  54.         s.sw = quadTree(pygame.Rect(s.rect.midleft, size))
  55.         s.se = quadTree(pygame.Rect(s.rect.center, size))
  56.         s.divided = True # the parent quadtree can no longer be divided
  57.  
  58.         # draw the boundry to help visualise what's going on
  59.         pygame.gfxdraw.hline(PD, int(s.rect.x), int(s.rect.right), int(s.rect.centery), (255, 255, 255))
  60.         pygame.gfxdraw.vline(PD, int(s.rect.centerx), int(s.rect.y), int(s.rect.bottom), (255, 255, 255))
  61.  
  62. # pygame display settings
  63. DR = pygame.Rect((0, 0, 800, 600)) # Display Rectangle
  64. HDW, HDH = DR.center # H = half
  65.  
  66. # set up pygame
  67. pygame.init()
  68. PD = pygame.display.set_mode(DR.size) # primary display based of the size of Display Rect (800, 600)
  69.  
  70. # create a quadtree instance and initialise it with the display rectange (size of the display)
  71. QT = quadTree(DR)
  72.  
  73. # previous position of the mouse pointer
  74. px, py = -1, -1
  75.  
  76. # press ESC to exit
  77. while True:
  78.     # process events
  79.     e = pygame.event.get()
  80.     if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
  81.  
  82.     # get position of the mouse
  83.     mx, my = pygame.mouse.get_pos()
  84.  
  85.     # if current mouse position is different to the previous mouse position
  86.     # and left mouse button has been pressed then...
  87.     if (mx, my) != (px, py) and pygame.mouse.get_pressed()[0]:
  88.         # set the previous mouse position to the current mouse position
  89.         px, py = (mx, my)
  90.  
  91.         # create a vector from the mouse location with a random offset on the x and y axis
  92.         rx, ry = (mx + random.randint(-40, 40), my + random.randint(-40, 40))
  93.  
  94.         # create a point using pygame's vector2
  95.         p = pygame.math.Vector2(rx, ry)
  96.  
  97.         # insert the point into the quadtree
  98.         QT.insert(p)
  99.  
  100.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement