-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsmith.py
74 lines (61 loc) · 1.96 KB
/
smith.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
import math
import random
from drawing import cairo_context
from helpers import color, range2d
PI = math.pi
PI2 = math.pi / 2
eps = 0.5
class SmithTile:
def init_tile(self, ctx, wh, bgfg=None):
if bgfg is None:
bgfg = [color(1), color(0)]
eps = 0
ctx.rectangle(0 - eps, 0 - eps, wh + eps, wh + eps)
ctx.set_source_rgba(*bgfg[0])
ctx.fill()
ctx.set_source_rgba(*bgfg[1])
def draw(self, ctx, wh):
...
class SmithLeftTile(SmithTile):
def draw(self, ctx, wh, bgfg=None):
self.init_tile(ctx, wh, bgfg)
wh2 = wh // 2
ctx.move_to(0 - eps, wh2)
ctx.arc(0 - eps, wh + eps, wh2 + eps, -PI2, 0)
ctx.line_to(0 - eps, wh + eps)
ctx.close_path()
ctx.fill()
ctx.move_to(wh + eps, wh2)
ctx.arc(wh + eps, 0 - eps, wh2 + eps, PI2, PI)
ctx.line_to(wh + eps, 0 - eps)
ctx.close_path()
ctx.fill()
class SmithRightTile(SmithLeftTile):
def draw(self, ctx, wh, bgfg):
wh2 = wh / 2
ctx.save()
ctx.translate(wh2, wh2)
ctx.rotate(PI2)
ctx.translate(-wh2, -wh2)
super().draw(ctx, wh, bgfg)
ctx.restore()
def smith(width=400, height=200, tilew=40, grid=False, gap=0, seed=None):
rand = random.Random(seed)
with cairo_context(width, height) as ctx:
tiles = [SmithLeftTile(), SmithRightTile()]
bgfgs = [
[color(1), color(0)],
[color(0), color(1)],
]
for ox, oy in range2d(width // tilew, height // tilew):
ctx.save()
ctx.translate(ox * (tilew + gap), oy * (tilew + gap))
coin = rand.choice([0, 1])
tiles[coin].draw(ctx, tilew, bgfgs[(ox + oy + coin) % 2])
if grid:
ctx.set_line_width(0.1)
ctx.rectangle(0, 0, tilew, tilew)
ctx.set_source_rgb(0, 0, 0)
ctx.stroke()
ctx.restore()
return ctx