• Create Account

# ufis

Member Since 22 Oct 2012
Offline Last Active Private

### In Topic: Store quadtree data in db

23 October 2012 - 05:00 AM

In general, I would suggest converting the quadtree to a geohash by indexing each of your quadtree's nodes with a binary representation of it's location (00 for NE, 01 for NW, etc.) and the prefixing each of the child nodes with the parent's index (e.g. the key 0111 would be the SE tree/leaf of the NW tree of the root). By then returning a string representation of the hash, you'll be able to easily store the data in a database while still being able to retrieve and modify it. Hopefully this helps.

This is helpful for storing and retreiving a single node.

My problem is to find a way to represent the whole grid as a single string, and mark those areas that has been explored.

Somehow the following is stuck in my head, but it also has problems (I'll mention those later)

Lets take a 4x4 grid, so the quad tree will be 3 levels deep.

I'll order the quads NW, NE, SE, SW.

Empty grid.
```---------
| | | | |
---------
| | | | |
---------
| | | | |
---------
| | | | |
---------
```
As none of the leaf nodes has been explored the whole grid is represented by the value 0.

Now our hero starts out. This means he has 1 leaf explored.
```---------
| | | | |
---------
| | | | |
---------
| | |x| |
---------
| | | | |
---------```
So the idea is to mark a node that has been explored and all its parents with a 1.
When we write the string when we get a 0 we do not delve into the child nodes as we know they are all empty. When we get a 1, we list the 4 children and their values.
So we will have
1 (root)
- 0 NW
- 0 NE
- 1 SE
-- 1 NW (this is where our hero finds himself)
-- 0 NE
-- 0 SE
-- 0 SW
- 0 SW
So we get the final string of : 100110000

After our hero walks around a bit we get this map
```---------
| | |x| |
---------
| | |x| |
---------
| |x|x|x|
---------
| |x|x|x|
---------
```
And with that the string: 10110011111110110

And when he has completed exploring the whole map we get : 111111111111111111111 (21 characters)

Adding another level (8x8 grid) adds 64 more numbers, 5 levels deep (16x16 grid) adds another 256, 6 levels (32x32 grid) another 1024 ... and you can see where this is going.

So the string is getting a bit big when you get to a 1024x1024 grid. The more you explore the longer the string gets.

I guess I could use a 2 to mark a node when all it's children has been explored. That will make the string shorter.

But the other problem is still on how to update this string, as the hero explores, in a way that is not too resource or time intensive.

Any ideas?

PARTNERS