Advertisement
ezak

Quadtree

Apr 1st, 2012
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Use this function to perform your initial setup
  4. function setup()
  5.     noSmooth()
  6.     noFill()
  7.     strokeWidth(1)
  8.    
  9.     -- set quadtree depth (number of subdivisions)
  10.     depth = 4
  11.    
  12.     size = 512
  13.     map = Quadtree(0, 0, size, size, depth)
  14.    
  15.     ratio = WIDTH/size
  16.     touches = {}
  17.     fill(255, 255, 255, 255)
  18.     print("Quadtree loaded...")
  19. end
  20.  
  21. -- This function gets called once every frame
  22. function draw()
  23.     background(0, 0, 0, 255)
  24.     text("Touch anywhere on screen", WIDTH/2, HEIGHT-10)
  25.    
  26. -- scale to screen width
  27.     scale(ratio)
  28.    
  29. -- draw for every touch
  30.     for k,touch in pairs(touches) do
  31.         map:draw(touch.x/ratio, touch.y/ratio, depth)
  32.     end
  33. end
  34.  
  35. function touched(touch)
  36.     if touch.state == ENDED then
  37.         touches[touch.id] = nil
  38.     else
  39.         touches[touch.id] = touch
  40.     end
  41. end
  42. --# Quadtree
  43. Quadtree = class()
  44.  
  45. function Quadtree:init(xmin, ymin, xmax, ymax, d)
  46.     self.xmin = xmin
  47.     self.xmax = xmax
  48.     self.ymin = ymin
  49.     self.ymax = ymax
  50.     self.children = {}
  51.     self.chunk = mesh()
  52.  
  53. -- generate texture
  54.     local w = self.xmax - self.xmin
  55.     local h = self.ymax - self.ymin
  56.     local x = self.xmin + w/2
  57.     local y = self.ymin + w/2
  58.    
  59.     local t = image(w, h)
  60.     setContext(t)
  61.     stroke(255, 255, 255, 255)
  62.     rect(0, 0, w, h)
  63.     self.chunk.texture = t
  64.     self.chunk:addRect(x, y, w, h)
  65.    
  66. -- Subdivide until desired depth is reached
  67.     if d > 0 then
  68.         d = d - 1
  69.         self.chunk:setColors(255, 0, 0, 255)
  70.        
  71.         local xmoy = (xmin + xmax)/2
  72.         local ymoy = (ymin + ymax)/2
  73.        
  74.         self.children[1] = Quadtree(xmin, ymin, xmoy, ymoy, d)
  75.         self.children[2] = Quadtree(xmin, ymoy, xmoy, ymax, d)
  76.         self.children[3] = Quadtree(xmoy, ymoy, xmax, ymax, d)
  77.         self.children[4] = Quadtree(xmoy, ymin, xmax, ymoy, d)
  78.     else
  79.         self.chunk:setColors(0, 0, 255, 255)
  80.     end
  81.    
  82. end
  83.  
  84. function Quadtree:draw(x, y, d)
  85. -- Draw child if the "touch" is inside its bounds
  86.     if x>=self.xmin and x<=self.xmax and y>=self.ymin and y<=self.ymax then        
  87.         if d > 0 then
  88.             self.chunk:draw()
  89.            
  90.             d = d - 1
  91.             self.children[1]:draw(x, y, d)
  92.             self.children[2]:draw(x, y, d)
  93.             self.children[3]:draw(x, y, d)
  94.             self.children[4]:draw(x, y, d)
  95.         else
  96.             self.chunk:draw()
  97.         end
  98.     end    
  99.        
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement