forked from shashi/escher-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncgeo.jl
361 lines (236 loc) · 9.31 KB
/
funcgeo.jl
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using Markdown
using Interact
using Compose
Compose.set_default_graphic_size(2inch, 2inch)
codecell(input, output=eval(parse("begin $input end")); f = x -> x, layout=hbox) =
layout(
codemirror(input),
hskip(1em),
vskip(1em),
output |> f
) |> packacross(center) |> packitems(center)
points_f = [
(.1, .1),
(.9, .1),
(.9, .2),
(.2, .2),
(.2, .4),
(.6, .4),
(.6, .5),
(.2, .5),
(.2, .9),
(.1, .9),
(.1, .1)
]
f = compose(context(), stroke("black"), line(points_f))
rot(pic) = compose(context(rotation=Rotation(-deg2rad(90))), pic)
flip(pic) = compose(context(mirror=Mirror(deg2rad(90), 0.5w, 0.5h)), pic)
above(m, n, p, q) =
compose(context(),
(context(0, 0, 1, m/(m+n)), p),
(context(0, m/(m+n), 1, n/(m+n)), q))
above(p, q) = above(1, 1, p, q)
beside(m, n, p, q) =
compose(context(),
(context(0, 0, m/(m+n), 1), p),
(context(m/(m+n), 0, n/(m+n), 1), q))
beside(p, q) = beside(1, 1, p, q)
over(p, q) = compose(context(),
(context(), p), (context(), q))
rot45(pic) =
compose(context(0, 0, 1/sqrt(2), 1/sqrt(2),
rotation=Rotation(-deg2rad(45), 0w, 0h)), pic)
# Utility function to zoom out and look at the context
zoomout(pic) = compose(context(),
(context(0.2, 0.2, 0.6, 0.6), pic),
(context(0.2, 0.2, 0.6, 0.6), fill(nothing), stroke("black"), strokedash([0.5mm, 0.5mm]),
polygon([(0, 0), (1, 0), (1, 1), (0, 1)])))
function read_path(p_str)
tokens = [try parsefloat(x) catch symbol(x) end for x in split(p_str, r"[\s,]+")]
path(tokens)
end
fish = compose(context(units=UnitBox(260, 260)), stroke("black"),
read_path(strip(readall("fish.path"))))
rotatable(pic) = @manipulate for θ=0:0.001:2π
compose(context(rotation=Rotation(θ)), pic)
end
blank = compose(context())
fliprot45(pic) = rot45(compose(context(mirror=Mirror(deg2rad(-45))),pic))
fish2 = fliprot45(fish)
fish3 = rot(rot(rot(fish2)))
t = over(fish, over(fish2, fish3))
u = over(over(fish2, rot(fish2)),
over(rot(rot(fish2)), rot(rot(rot(fish2)))))
quartet(p, q, r, s) =
above(beside(p, q), beside(r, s))
cycle(p) =
quartet(p, rot(p), rot(rot(p)), rot(rot(rot(p))))
nonet(p, q, r,
s, t, u,
v, w, x) =
above(1,2,beside(1,2,p,beside(1,1,q,r)),
above(1,1,beside(1,2,s,beside(1,1,t,u)),
beside(1,2,v,beside(1,1,w,x))))
side1 = quartet(blank, blank, rot(t), t)
side2 = quartet(side1,side1,rot(t),t)
side(n) =
if n == 1 side1 # basis
else quartet(side(n-1),side(n-1),rot(t),t) # induction
end
corner1 = quartet(blank,blank,blank,u)
corner2 = quartet(corner1,side1,rot(side1),u)
corner(n) =
n == 1 ? corner1 :
quartet(corner(n-1), side(n-1), rot(side(n-1)), u)
squarelimit(n) =
nonet(corner(n), side(n), rot(rot(rot(corner(n)))),
rot(side(n)), u, rot(rot(rot(side(n)))),
rot(corner(n)), rot(rot(side(n))), rot(rot(corner(n))))
midsize(p) = drawing(4inch, 4inch, p)
largesize(p) = drawing(10inch, 10inch, p)
function main(window)
drawing(10inch, 10inch, squarelimit(3))
push!(window.assets, "codemirror")
md"""
$(title(3, "Functional Geometry") |> fontcolor("#333"))
$(vskip(1em))
This document is a literate programming summary of a paper called *Functional Geometry* by Peter Henderson ([original (1982)](users.ecs.soton.ac.uk/peter/funcgeo.pdf), [revisited (2002)](https://cs.au.dk/~hosc/local/HOSC-15-4-pp349-365.pdf)). It is an attempt to reconstruct MC Escher's woodcut *Square Limit* using functional programming.
A picture as a value not too dissimilar to numbers. With a set of operators that do simple things with pictures we create an algebra of pictures and build up to the final image seen below.
$(vskip(1em))
$(image("http://i.imgur.com/LjRzmNM.png") |> hbox |> packitems(center))
# A picture
A `picture` is a *denotation* of something to draw.
> A picture is an example of a complex object that can be described in terms of its parts. Yet a picture needs to be rendered on a printer or a screen by a device that expects to be given a sequence of commands. Programming that sequence of commands directly is much harder than having an application generate the commands automatically from the simpler, denotational description.
e.g. The value of f here denotes the picture of the letter F
$(codecell("f"))
## Basic Operations on Pictures
We begin specifying the algebra of pictures we will use to describe *Square Limit* with a few operations that operate on pictures to give other pictures, namely:
* `rot : picture → picture`
* `flip : picture → picture`
* `rot45 : picture → picture`
* `above : picture × picture → picture`
* `above : int × int × picture × picture → picture`
* `beside : picture × picture → picture`
* `beside : int × int × picture × picture → picture`
* `over : picture → picture`
## Rotate and flip
### rot : picture → picture
Rotate a picture anti-clockwise by 90°
$(codecell("rot(f)"))
### flip : picture → picture
Flip a picture along its virtical center axis
$(
hbox(
codecell("flip(f)"),
hskip(3em),
codecell("rot(flip(f))"),
)
)
### fliprot45 : picture → picture
rotate the picture anti-clockwise by 45°, then flip it across the new virtical axis. In the paper this is implemented as `flip(rot45(fish))`. This function is rather specific to the problem at hand.
$(codecell("fliprot45(fish)", fliprot45(fish) |> zoomout))
## Juxtaposition
#### `above : picture × picture → picture`
place a picture above another.
$(codecell("above(f, f)"))
#### `above : int × int × picture × picture → picture`
given `m`, `n`, `picture1` and `picture2`, return a picture where `picture1` is placed above `picture2` such that their heights occupy the total height in m:n ratio
$(codecell("above(1, 2, f, f)"))
#### `beside : picture × picture → picture`
Similar to `above` but in the left-to-right direction.
$(codecell("beside(f, f)"))
#### `beside : int × int × picture × picture → picture`
$(codecell("beside(1, 2, f, f)"))
$(codecell("above(beside(f, f), f)"))
## Superposition
#### `over : picture → picture`
place a picture upon another
$(codecell("over(f, flip(f))"))
# Square Limit
## The Fish
We will now study some of the properties of the fish.
$(codecell("fish |> zoomout"))
$(codecell("over(fish, rot(rot(fish))) |> zoomout"))
## Tiles
There is a certain kind of arrangement that is used to tile parts of the image.
`t`:
$(codecell(
"fish2 = fliprot45(fish)
fish3 = rot(rot(rot(fish2)))
t = over(fish, over(fish2, fish3))
t |> zoomout
"))
`u`:
$(codecell(
"u = over(over(fish2, rot(fish2)),
over(rot(rot(fish2)), rot(rot(rot(fish2)))))
u |> zoomout
"))
## Tesselations
`quartet` tiles 4 images in a 2x2 grid
$(codecell(
"quartet(p, q, r, s) =
above(beside(p, q), beside(r, s))
quartet(f,flip(f),rot(f),f)
"))
Notice how the fish interlock without leaving out any space in between them. Escher FTW.
`cycle` is a quartet of the same picture with each successive tile rotated by 90° anti-clockwise
$(codecell(
"cycle(p) =
quartet(p, rot(p), rot(rot(p)), rot(rot(rot(p))))
cycle(f)
"))
A nonet is a 3 × 3 grid of 9 pictures.
$(codecell(
"nonet(p, q, r,
s, t, u,
v, w, x) =
above(1,2,beside(1,2,p,beside(1,1,q,r)),
above(1,1,beside(1,2,s,beside(1,1,t,u)),
beside(1,2,v,beside(1,1,w,x))))
nonet(f, f, f, f, f, f, f, f, f) "))
## Sides and Corners of The Square Limit
Note: `blank` denotes a blank `picture`
There is a certain pattern which makes up the mid region of each of the four edges of the image. We will call this arrangement `side`
the 1 in `side1` represents 1 level of recursion. This is the simplest side.
$(codecell(
"side1 = quartet(blank, blank, rot(t), t)
side1 |> zoomout", f=midsize, layout=vbox))
A side that is 2 levels deep.
$(codecell(
"side2 = quartet(side1,side1,rot(t),t)
side2 |> zoomout", f=midsize, layout=vbox))
n-levels deep:
$(codecell(
"side(n) =
if n == 1 side1 # basis
else quartet(side(n-1),side(n-1),rot(t),t) # induction
end
side(3) |> zoomout
", f=midsize, layout=vbox))
Similarly, there is a certain kind of arrangement which makes up the corners of the artwork.
A `corner` 1 level deep is simply
$(codecell(
"corner1 = quartet(blank,blank,blank,u)
corner1 |> zoomout", f=midsize, layout=vbox))
A corner 2 levels deep, it is built using corner1, side1 and u.
$(codecell(
"corner2 = quartet(corner1,side1,rot(side1),u)
corner2 |> zoomout", f=midsize, layout=vbox))
An n level deep corner.
$(codecell(
"corner(n) =
n == 1 ? corner1 :
quartet(corner(n-1), side(n-1), rot(side(n-1)), u)
corner(3) |> zoomout", f=midsize, layout=vbox))
# Square limit
Having built up the algebra to describe *Square Limit*, we can now precisely denote it. Square limit is a nonet of right angled rotations of `corner` at the corners, `side` at the sides and `u` in the center. The precise algebra and the code are identical:
$(codecell(
"squarelimit(n) =
nonet(corner(n), side(n), rot(rot(rot(corner(n)))),
rot(side(n)), u, rot(rot(rot(side(n)))),
rot(corner(n)), rot(rot(side(n))), rot(rot(corner(n))))
squarelimit(3)", f=largesize, layout=vbox))
*The End.*
""" |> Escher.pad([top, bottom], 4em) |> maxwidth(50em) |> vbox |> packacross(center)
end