-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathottobot.py
More file actions
260 lines (213 loc) · 6.6 KB
/
Copy pathottobot.py
File metadata and controls
260 lines (213 loc) · 6.6 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# encoding: utf-8
from AppKit import NSFontManager
from plotdevice.util import random, choice
COMP_WIDTH = 500
COMP_HEIGHT = 500
XCOORD = 1
YCOORD = 2
XSIZE = 3
YSIZE = 4
ROTATION = 5
SCALE = 6
CONTROLPOINT = 7
COLOR = 8
STROKEWIDTH = 9
LOOP = 10
GRIDDELTA = 12
GRIDCOUNT = 13
GRIDWIDTH = 14
GRIDHEIGHT = 15
SKEW = 16
STARPOINTS = 17
class Context:
def __init__(self):
self._indent = 0
self._grid = False
def indent(self):
self._indent += 1
def dedent(self):
self._indent -= 1
def spaces(self):
return " " * self._indent
def inGrid(self):
return self._grid
def nrReally(ctx, numberclass):
if numberclass == XCOORD:
if ctx.inGrid():
#return "x"
return "x + %s" % nr(ctx,GRIDDELTA)
else:
return random(-COMP_WIDTH/2,COMP_WIDTH/2)
elif numberclass == YCOORD:
if ctx.inGrid():
#return "y"
return "y + %s" % nr(ctx,GRIDDELTA)
else:
return random(-COMP_HEIGHT/2,COMP_HEIGHT/2)
elif numberclass == XSIZE:
return random(0,COMP_WIDTH)
elif numberclass == YSIZE:
return random(0,COMP_HEIGHT)
elif numberclass == ROTATION:
return random(0,360)
elif numberclass == SCALE:
return random(0.5,1.5)
elif numberclass == CONTROLPOINT:
return random(-100,100)
elif numberclass == COLOR:
return random()
elif numberclass == STROKEWIDTH:
return random(1,20)
elif numberclass == LOOP:
return random(2, 20)
elif numberclass == GRIDDELTA:
return random(-100,100)
elif numberclass == GRIDCOUNT:
return random(2, 10)
elif numberclass == GRIDWIDTH:
return 20
return random(1,100)
elif numberclass == GRIDHEIGHT:
return 20
return random(1, 100)
elif numberclass == SKEW:
return random(1,80)
elif numberclass == STARPOINTS:
return random(2,100)
def nr(ctx, numberclass):
if not ctx.inGrid() and random() > 0.5:
return "random(%s)" % nrReally(ctx, numberclass)
else:
return "%s" % nrReally(ctx, numberclass)
### DRAWING COMMANDS ###
def genDraw(ctx):
fn = choice((genRect,genOval,genArrow,genStar,genPath))
return fn(ctx)
def genRect(ctx):
return ctx.spaces() + """rect(%s,%s,%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD),nr(ctx,XSIZE),nr(ctx,YSIZE))
def genOval(ctx):
return ctx.spaces() + """oval(%s,%s,%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD),nr(ctx,XSIZE),nr(ctx,YSIZE))
def genArrow(ctx):
return ctx.spaces() + """arrow(%s,%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD),nr(ctx,XSIZE))
def genStar(ctx):
return ctx.spaces() + """star(%s,%s,%s,%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD),nr(ctx,STARPOINTS),nr(ctx,XSIZE),nr(ctx,XSIZE))
def genPath(ctx):
s = ctx.spaces() + """beginpath(%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD))
for i in range(random(1,10)):
s += genPathDraw(ctx)
s += ctx.spaces() + """endpath()\n"""
return s
def genPathDraw(ctx):
fn = choice((genLineto, genCurveto))
return fn(ctx)
def genLineto(ctx):
return ctx.spaces() + """lineto(%s,%s)\n""" % (nr(ctx,XCOORD),nr(ctx,YCOORD))
def genCurveto(ctx):
return ctx.spaces() + """curveto(%s,%s,%s,%s,%s,%s)\n""" % (
nr(ctx,XCOORD),nr(ctx,YCOORD),nr(ctx,CONTROLPOINT),nr(ctx,CONTROLPOINT),nr(ctx,CONTROLPOINT),nr(ctx,CONTROLPOINT))
### TRANSFORM ###
def genTransform(ctx):
fn = choice((genRotate, genTranslate, genScale, genSkew, genReset))
return fn(ctx)
def genRotate(ctx):
return ctx.spaces() + """rotate(%s)\n""" % nr(ctx,ROTATION)
def genTranslate(ctx):
return ctx.spaces() + """translate(%s,%s)\n""" % (nr(ctx,XCOORD), nr(ctx,YCOORD))
def genScale(ctx):
return ctx.spaces() + """scale(%s)\n""" % (nr(ctx,SCALE))
def genSkew(ctx):
return ctx.spaces() + """skew(%s)\n""" % (nr(ctx,SKEW))
def genReset(ctx):
return ctx.spaces() + """reset()\n"""
### COLOR ###
def genColor(ctx):
fn = choice((genFill,genFill,genFill,genFill,genFill,genFill,genStroke,genStroke,genStroke,genNofill,genNostroke,genStrokewidth))
return fn(ctx)
def genFill(ctx):
return ctx.spaces() + """fill(%s,%s,%s,%s)\n""" % (nr(ctx,COLOR),nr(ctx,COLOR), nr(ctx,COLOR), nr(ctx,COLOR))
def genStroke(ctx):
return ctx.spaces() + """stroke(%s,%s,%s,%s)\n""" % (nr(ctx,COLOR), nr(ctx,COLOR), nr(ctx,COLOR), nr(ctx,COLOR))
def genNofill(ctx):
return ctx.spaces() + """nofill()\n"""
def genNostroke(ctx):
return ctx.spaces() + """nostroke()\n"""
def genStrokewidth(ctx):
return ctx.spaces() + """strokewidth(%s)\n""" % nr(ctx,STROKEWIDTH)
### LOOP ###
def genLoop(ctx):
fn = choice((genFor, genGrid))
return fn(ctx)
def genFor(ctx):
if ctx._indent >= 2: return ""
s = ctx.spaces() + """for i in range(%s):\n""" % nr(ctx,LOOP)
ctx.indent()
for i in range(random(5)):
s += genStatement(ctx)
s += genVisual(ctx)
ctx.dedent()
return s
def genGrid(ctx):
if ctx.inGrid(): return ""
s = ctx.spaces() + """for x, y in grid(%s,%s,%s,%s):\n""" % (nr(ctx,GRIDCOUNT), nr(ctx,GRIDCOUNT), nr(ctx,GRIDWIDTH), nr(ctx,GRIDHEIGHT))
ctx.indent()
ctx._grid = True
for i in range(random(5)):
s += genStatement(ctx)
s += genVisual(ctx)
ctx.dedent()
ctx._grid = False
return s
### MAIN ###
def genVisual(ctx):
fn = choice((genDraw,))
return fn(ctx)
def genStatement(ctx):
fn = choice((genVisual,genLoop,genColor,genTransform))
return fn(ctx)
def genProgram():
s = """# This code is generated with OTTOBOT,
# the automatic PlotDevice code generator.
size(%s, %s)
translate(%s, %s)
colormode(HSB)
""" % (COMP_WIDTH, COMP_HEIGHT, COMP_WIDTH/2, COMP_HEIGHT/2)
ctx = Context()
for i in range(random(10,20)):
s += genStatement(ctx)
return s
def genTemplate(kind='sketch'):
if kind=='sketch':
return """size(512, 512)
background(1)
rect(20, 15, WIDTH-40, 1)
text("Welcome to PlotDevice", 20, 40)
"""
elif kind=='anim':
return """# to create an animation, call speed() with a
# frame-rate value. This has the side-effect of
# running setup, draw, and stop in the pattern
# described below:
speed(30)
# runs once before the first frame of animation
def setup():
size(500, 500)
background(1)
print("start")
# runs repeatedly until you cancel with ⌘.
def draw():
text("Frame %i"%FRAME, 20,40)
# runs once after the animation was cancelled
def stop():
print("done")
"""
elif kind=='ottobot':
return genProgram()
else:
return ""
if __name__ == '__main__':
print(genProgram())