QuadTree - drawing a Circle

Monkey Forums/Monkey Code/QuadTree - drawing a Circle

NoOdle(Posted 2012) [#1]
As the title suggests. Press SpaceKey to toggle the display.




slenkar(Posted 2012) [#2]
whoa nice
why do you use quadtrees to draw a circle? they seem like they could be useful for something


NoOdle(Posted 2012) [#3]
why do you use quadtrees to draw a circle?


lol why not? Its a fun visual representation, and yes they have lots of uses! Some taken from wikipedia:

+ Spatial indexing
+ Efficient collision detection in two dimensions
+ View frustum culling of terrain data
+ Storing sparse data, such as a formatting information for a spreadsheet or for some matrix calculations
+ Solution of multidimensional fields (computational fluid dynamics, electromagnetism)
+ Conway's Game of Life simulation program
+ State estimation
+ Quadtrees are also used in the area of fractal image analysis


DruggedBunny(Posted 2012) [#4]
Looks cool!


NoOdle(Posted 2012) [#5]
another update, press 1 - 4 to change the shape and press space to toggle display




slenkar(Posted 2012) [#6]
Nice, I think I need quadtrees to see if many people are close enough on a map.

Would you mind giving a small explanation of how to do that with quadtrees?


JIM(Posted 2012) [#7]
Depending on your particular case I would advise against quadtrees.

Where to use them:

Large area to cover, but very little space occupied with objects mostly static.

Where not to use them:

Small/medium area to cover with lots of objects that constantly move.

A quadtree essentially works by splitting an area into 4 every time there's too much data in that area. Suppose you want to keep a maximum of 16 objects in an area.

1. You start with a big area covering your map and consider all units in there.
2. Less than 16 units? You are done.
3. More than 16 units? Split the are into 4 equal parts and redistribute units. For each of those areas, go back to 2.

Why isn't this a very good idea with lots of moving units? If your units end up walking around the whole map, you will end up with a maximum split on your quad tree.

This is quite bad because at the lowest level you will essentially have a grid, except it's not all in one place in memory, it's all over the place and besides that, it has a whole tree structure above it that's mostly useless.

That's why I would go for a "bucket grid" if you may. Preallocate a grid of cells, each cell having space for n unit pointers (8, 16, etc.) and a counter (to avoid resizing the "bucket"). Updating is extremely easy. Each unit can instantly find it's proper bucket (x / grid_size, y / grid_size).
It's equally easy to find the neighbours of any cell.

To add to a cell you do something simple like:

grid[x].storage[grid[x].crt_count] = unit_pointer
grid[x].crt_count += 1


Removing is easier:

grid[x].crt_count -= 1


All of them are very fast operations that only change values in memory without messing it up :)

The only downside is the memory usage for preallocation. Try to find a balance to have as few cells as possible, but always make sure the bucket is large enough to hold the maximum possible units in that cell.

Again, depending on application, it might work to actually spill the bucket into the nearest neighbour that has enough room.

Whew! Wall of text. Hope that doesn't scare you :)


slenkar(Posted 2012) [#8]
yeah thats probably the right approach thanks,
basically only test distance between units that are in the same or neighbouring squares


NoOdle(Posted 2012) [#9]
oh sorry slenkar not sure how I didn't notice your question!! JIM is correct, although there are examples on the internet showing how to use quadtrees for proximity testing, the speed gained by using this is dependant on a good spread. Quite often brute force distance detection is as as fast if not faster in some extreme circumstances. I would recommend a grid based approach as well. Again, sorry for not replying sooner!