-
Notifications
You must be signed in to change notification settings - Fork 0
/
corners_smart.py
44 lines (29 loc) · 1.13 KB
/
corners_smart.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
#!/usr/bin/env python
import random
from geometer import *
def draw(canvas):
size = 256
north = lambda point: Point(point.x, point.y + size),
east = lambda point: Point(point.x + size, point.y),
south = lambda point: Point(point.x, point.y - size),
west = lambda point: Point(point.x - size, point.y)
used = {}
g = SquareGrid(canvas.center, 256, 5, 5)
for p in g.points:
# check to see if we've seen two already
num_used = used.setdefault(p, 0)
if num_used == 2:
continue
candidates = [
g.closest_point_to(func(p)) for func in [north, east, south, west]]
random.shuffle(candidates)
while num_used <= 2 and len(candidates):
candidate = candidates.pop()
num_candidate_used = used.setdefault(candidate, 0)
if num_candidate_used == 2:
continue
num_used += 1
num_candidate_used += 1
used[candidate] = num_candidate_used
Line(p, candidate).draw(canvas)
used[p] = num_used