-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspatialhash.py
66 lines (53 loc) · 1.72 KB
/
spatialhash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import math
import itertools
class SpatialHash:
"""
Simple spatial-hash.
"""
def __init__(self, width, height, cellsize):
self.width = width
self.height = height
self.cellsize = cellsize
self.cells = [[Node(-1) for _ in range(math.ceil(height / cellsize))] for _ in range(math.ceil(width / cellsize))]
self.xcells = len(self.cells)
self.ycells = len(self.cells[0])
self.nodes = {}
def coords_to_index(self, coords):
return int(coords[0] // self.cellsize), int(coords[1] // self.cellsize)
def neighbours(self, coords):
location = self.coords_to_index(coords)
for dx, dy in itertools.product(range(-1, 2), range(-1, 2)):
node = self.cells[location[0] + dx][location[1] + dy].next
while node is not None:
yield node.id
node = node.next
def move(self, id, new_coords):
node = self.nodes.get(id)
if node is None:
node = Node(id)
self.nodes[id] = node
location = self.coords_to_index(new_coords)
node.attach(self.cells[location[0]][location[1]])
class Node:
"""
Nodes for a linked-list
"""
def __init__(self, id):
self.id = id
self.prev = None
self.next = None
def attach(self, start):
# remove contacts
if self.prev is not None:
self.prev.next = self.next
self.prev = None
if self.next is not None:
self.next.prev = self.prev
self.next = None
# attact to front
self.next = start.next
if self.next:
self.next.prev = self
self.prev = start
start.next = self
pass