HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

N-dimensional maze generation with octrees and pathfinding

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pathfindingoctreeswithmazeandgenerationdimensional

Problem

I did a maze thing ages ago that could support any number of dimensions, but it was very slow to generate. To get an idea of how it generally works with the multiplier and stuff, here's a gif of something I made with it:

One thing that always bugged me was I couldn't figure out pathfinding, and I thought there'd be some cool looking intricate paths between some stuff. I asked about that (since I'm stuck at home doing nothing due to having a broken wrist), and someone suggested linked lists, so I rewrote it all, and managed to get pathfinding between two points working fairly easily.

However, since I'd made the nearest neighbour check run by default, checking for collisions was a huge bottleneck. I tried to optimise it a little, by only doing the pythagoras stuff if the highest coordinate difference was within the combined size of two nodes, which did speed it up like 2x, but it was still quite slow.

Someone else suggested that's what you use trees for, and since I have no idea how KD trees work (and also the list is constantly being added to), I spent a good few hours yesterday making an octree that would work in any dimension, so I could find which nodes are near to then narrow down what the collision function has to check through. The result was like 2.5x faster at 1000 nodes, but exponentially going up to like 10x faster at 8000 nodes, which I'm pretty happy with.

I've just got it fully working and cleaned up now, so I'm looking for a bit of feedback on either my writing style or anything that could be improved. Also, before the line length is mentioned, I decided to do 100 instead of 80.

```
from __future__ import division
import random
import cPickle

class Node(object):
"""Store the data for each node."""

def __init__(self, id, location, size, distance=0,
parent=None, children=None, tree=None, neighbours=None):
self.id = id
self.location = tuple(location)
self.size = size
self.parent = parent

Solution

The only thing I noticed - you fell into the mutable-default trap, as in:

default_location=[]


This should be avoided; for more reading, see http://docs.python-guide.org/en/latest/writing/gotchas/

Code Snippets

default_location=[]

Context

StackExchange Code Review Q#118299, answer score: 2

Revisions (0)

No revisions yet.