forked from jakubcerveny/gilbert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gilbert2d.py
executable file
·80 lines (57 loc) · 1.99 KB
/
gilbert2d.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
def sgn(x):
return -1 if x < 0 else (1 if x > 0 else 0)
def _gilbert2d(x, y, ax, ay, bx, by):
"""
Generalized Hilbert ('gilbert') space-filling curve for arbitrary-sized
2D rectangular grids.
"""
w = abs(ax + ay)
h = abs(bx + by)
(dax, day) = (sgn(ax), sgn(ay)) # unit major direction
(dbx, dby) = (sgn(bx), sgn(by)) # unit orthogonal direction
if h == 1:
# trivial row fill
for i in range(0, w):
yield(x, y)
(x, y) = (x + dax, y + day)
return
if w == 1:
# trivial column fill
for i in range(0, h):
yield(x, y)
(x, y) = (x + dbx, y + dby)
return
(ax2, ay2) = (ax//2, ay//2)
(bx2, by2) = (bx//2, by//2)
w2 = abs(ax2 + ay2)
h2 = abs(bx2 + by2)
if 2*w > 3*h:
if (w2 % 2) and (w > 2):
# prefer even steps
(ax2, ay2) = (ax2 + dax, ay2 + day)
# long case: split in two parts only
yield from _gilbert2d(x, y, ax2, ay2, bx, by)
yield from _gilbert2d(x+ax2, y+ay2, ax-ax2, ay-ay2, bx, by)
else:
if (h2 % 2) and (h > 2):
# prefer even steps
(bx2, by2) = (bx2 + dbx, by2 + dby)
# standard case: one step up, one long horizontal, one step down
yield from _gilbert2d(x, y, bx2, by2, ax2, ay2)
yield from _gilbert2d(x+bx2, y+by2, ax, ay, bx-bx2, by-by2)
yield from _gilbert2d(x+(ax-dax)+(bx2-dbx), y+(ay-day)+(by2-dby),
-bx2, -by2, -(ax-ax2), -(ay-ay2))
def gilbert2d(width, height):
if width >= height:
yield from _gilbert2d(0, 0, width, 0, 0, height)
else:
yield from _gilbert2d(0, 0, 0, height, width, 0)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('width', type=int)
parser.add_argument('height', type=int)
args = parser.parse_args()
for x, y in gilbert2d(args.width, args.height):
print(x, y)