-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty.py
141 lines (116 loc) · 4.48 KB
/
pretty.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import skia
import matplotlib.pyplot as plt
class Pretty:
def boardbackground(self, canvas):
#shader = skia.Shaders.Blend(
# skia.BlendMode.kMultiply,
# skia.GradientShader.MakeRadial((self.size/2, self.size/2), 180.0, [0xff8888ff, 0xffff88ff]),
# skia.PerlinNoiseShader.MakeTurbulence(0.025, 0.025, 2, 0.0))
#canvas.drawPaint({'Shader': shader})
canvas.drawColor(skia.ColorWHITE)
# draw the background of cells
def cellbackground(self, canvas):
y = 0
for row in self.grid.grid:
x = 0
for c in row:
weight = self.grid.dist_weight(c)
dark = int(weight * 255)
light = int(127 + weight*127)
paint = skia.Paint(
Style=skia.Paint.kFill_Style,
AntiAlias=True,
StrokeWidth=10,
Color=skia.Color(dark,light,dark, 255))
rect = skia.Rect(x, y, x+self.step, y+self.step)
canvas.drawRect(rect, paint)
x += self.step
y += self.step
# draws current path through the maze
def path(self, canvas):
last_path = self.grid.get_last_path()
paint = skia.Paint(
Style=skia.Paint.kFill_Style,
AntiAlias=True,
StrokeWidth=10,
PathEffect=skia.CornerPathEffect.Make(self.step),
Color=skia.ColorBLUE)
# BUGBUG: skia red and blue channels are flipped ..
paint.setStrokeCap(skia.Paint.kRound_Cap)
# start
half_step = self.step // 2
x = last_path[0].column * self.step + half_step
y = last_path[0].row * self.step + half_step
xend = last_path[len(last_path)-1].column * self.step + half_step
yend = last_path[len(last_path)-1].row * self.step + half_step
canvas.drawCircle(x,y,half_step-5, paint)
#paint.setShader(skia.GradientShader.MakeLinear(
# points=[(x, y), (xend, yend)],
# colors=[skia.ColorBLUE, skia.ColorYELLOW]))
paint.setStyle(skia.Paint.kStroke_Style)
path = skia.Path()
path.moveTo(x, y)
for i in range(1, len(last_path)):
x = last_path[i].column * self.step + half_step
y = last_path[i].row * self.step + half_step
path.lineTo(x, y)
canvas.drawPath(path, paint)
def cellborder(self, canvas):
paint = skia.Paint(
Style=skia.Paint.kStroke_Style,
AntiAlias=True,
StrokeWidth=7,
Color=0xff000000)
paint.setStrokeCap(skia.Paint.kRound_Cap)
path = skia.Path()
y = 0
for row in self.grid.grid:
x = 0
for c in row:
if not c.east in c.links:
path.moveTo(x + self.step, y)
path.lineTo(x + self.step, y + self.step)
if not c.south in c.links:
path.moveTo(x + self.step, y + self.step)
path.lineTo(x, y + self.step)
x += self.step
y += self.step
canvas.drawPath(path, paint)
def boardborder(self, canvas):
paint = skia.Paint(
Style=skia.Paint.kStroke_Style,
AntiAlias=True,
StrokeWidth=10,
Color=skia.Color(0, 0, 0))
#paint.setShader(skia.GradientShader.MakeLinear(
# points=[(0.0, 0.0), (self.size, self.size)],
# colors=[0xFFFF0000, 0xFF888800]))
paint.setShader(skia.GradientShader.MakeSweep(
cx=128.0,
cy=128.0,
colors=[
skia.ColorCYAN,
skia.ColorMAGENTA,
skia.ColorYELLOW,
skia.ColorCYAN
]
))
rect = skia.Rect.MakeXYWH(0, 0, self.step*len(self.grid.grid[0]), self.step*len(self.grid.grid))
canvas.drawRect(rect, paint)
def __init__(self, ing):
self.inset = 15
self.size = 800
self.grid = ing
m = max(len(ing.grid), len(ing.grid[0]))
self.step = (self.size - self.inset * 2) / m
surface = skia.Surface(self.size, self.size)
canvas = surface.getCanvas()
canvas.translate(self.inset, self.inset)
self.boardbackground(canvas)
self.cellbackground(canvas)
self.path(canvas)
self.cellborder(canvas)
self.boardborder(canvas)
image = surface.makeImageSnapshot()
plt.imshow(image)
plt.show()