-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolyomino_app.py
128 lines (105 loc) · 4.16 KB
/
polyomino_app.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
from tkinter import *
import math
import polyomino as _mino
SYM_OPTS = ["free", "one-sided", "fixed"]
SYM_COLORS = {'|-\\/%@+XO': "tan",
'|-%+': "magenta",
'\\/%X': "yellow",
'%@': "cyan",
'|': "red",
'-': "red",
'\\': "green",
'/': "green",
'%': "blue",
'?': "gray"}
CANVAS_WIDTH = 800
CANVAS_HEIGHT = 600
def draw_mino(canvas, mino, x, y, size, fill):
"""Draw the polyomino on the specified canvas objecct."""
for i, j in mino:
canvas.create_rectangle([x + size*i, y+size*j,
x+size*(i+1), y+size*(j+1)],
fill=fill)
class PolyominoApp(Frame):
def __init__(self, master):
main = Frame(master)
main.pack()
# Size
textframe = Frame(main)
textframe.pack()
Label(textframe, text="Size: ").pack(side=LEFT)
self.scale_size = Scale(textframe, from_=0, to=11, orient=HORIZONTAL)
self.scale_size.pack(side=LEFT)
# Make the symmetry-choice buttons
symframe = Frame(main)
symframe.pack()
self.sym_value = IntVar()
self.sym_value.set(0)
for index, opt in enumerate(SYM_OPTS):
button = Radiobutton(symframe,
text=opt,
variable=self.sym_value,
value=index)
button.pack(anchor=W, side=LEFT)
# Make the "highlight symmetries" checkbox
self.symcolor_value = IntVar()
self.check_symcolor = Checkbutton(main, text="Highlight symmetries",
variable=self.symcolor_value)
self.check_symcolor.pack()
# Make the submit button
self.btn_submit = Button(main, text="Generate", command=self.submit)
self.btn_submit.pack()
# Canvas to show the results
canvasframe = Frame(main)
canvasframe.pack()
yscroll = Scrollbar(canvasframe)
yscroll.pack(side=RIGHT, fill=Y)
self.canvas = Canvas(canvasframe, bg="white",
width=CANVAS_WIDTH, height=CANVAS_HEIGHT,
scrollregion=(0,0,CANVAS_WIDTH,CANVAS_HEIGHT*2),
yscrollcommand=yscroll.set)
self.canvas.pack(side=LEFT, fill=BOTH)
yscroll.config(command=self.canvas.yview)
def submit(self):
# clear the canvas
self.canvas.delete(ALL)
## try:
n = int(self.scale_size.get())
## except ValueError:
## self.canvas.create_text(100, 100,
## text="Must input number", fill="red")
## return
sym = self.sym_value.get()
symcolor = self.symcolor_value.get()
if sym == 0:
minos = sorted(_mino.free(_mino.generate(n)), key=_mino.mino_key)
elif sym == 1:
minos = sorted(_mino.one_sided(_mino.generate(n)), key=_mino.mino_key)
else:
minos = sorted(_mino.generate(n), key=_mino.mino_key)
text = ("There are {0} {1} polyominoes of order {2}".format(
len(minos), SYM_OPTS[sym], n))
self.canvas.create_text(CANVAS_WIDTH//2, 25, text=text)
# Determine sizes
size = 5
padding = 2
margin = 40
minos_per_line = (CANVAS_WIDTH - margin * 2) // ((n + padding) * size)
ypos = margin
scroll_height = 2*margin + (n+padding)*size*(len(minos)//minos_per_line)
self.canvas.config(scrollregion=(0,0,CANVAS_WIDTH, scroll_height))
while minos:
for i in range(minos_per_line):
if not minos:
break
xpos = margin + ((n + padding) * size) * i
mino = minos.pop()
draw_mino(self.canvas, mino,
xpos, ypos, size,
fill=(SYM_COLORS[mino.symmetry()]
if symcolor else "gray"))
ypos += (n+padding) * size
root = Tk()
root.wm_title("Polyomino App")
app = PolyominoApp(root)
root.mainloop()