-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsystem.py
executable file
·63 lines (48 loc) · 1.54 KB
/
lsystem.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
#!/usr/bin/python
import sys, json, getopt
import Tkinter as tk
from lsys import LSystemVisualiser, LGrammar
MIN_WIDTH = 300
MIN_HEIGHT = 300
def usage():
sys.stderr.write("USAGE: %s -c <config.json> -g <WIDTH>x<HEIGHT> [-h]\n"
% (sys.argv[0]))
sys.exit(1)
def main(cfg, width, height):
master = tk.Tk()
master.geometry("%dx%d%+d%+d" % (width, height, 0, 0))
canvas = tk.Canvas(master, width=width, height=height)
canvas.pack()
try:
grammar = LGrammar(cfg['rules'])
lvis = LSystemVisualiser(canvas, cfg['angle'],
grammar.generate(cfg['axiom'],
cfg['iters']))
lvis.draw()
except ValueError as err:
sys.stderr.write("Error: " + str(err) + "\n")
sys.exit(1)
master.update()
tk.mainloop()
if __name__ == '__main__':
cfg, width, height = (None, None, None)
try:
opts, _ = getopt.getopt(sys.argv[1:], "c:g:h")
except getopt.GetoptError as err:
sys.stderr.write("Error: " + str(err) + "\n")
usage()
for o, a in opts:
if o == '-c':
cfg = json.load(open(a))
elif o == '-g':
try:
width, height = [int(i) for i in a.split("x")]
except ValueError:
usage()
width = max(width, MIN_WIDTH)
height = max(height, MIN_HEIGHT)
else:
usage()
if not any([cfg, width, height]):
usage()
main(cfg, width, height)