thirteen recursion. recursion ► [define horizontal-array [object spacing count → [if [= count 1]...
Post on 19-Dec-2015
220 views
TRANSCRIPT
![Page 1: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/1.jpg)
thirteen
recursion
![Page 2: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/2.jpg)
Recursion
► [define horizontal-array [object spacing count → [if [= count 1] object
[group object [translate [point spacing 0]
[horizontal-array object spacing [− count 1]]]]]]]
► [group [box 400 100] [horizontal-array [box 20 20] 30 6]]
►
![Page 3: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/3.jpg)
Wait a minute …
We’ve defined horizontal-array in terms of itself Isn’t that circular?
Well, sort of … We’ve defined how to make an array of 3 objects in terms of an array of 2 objects We’ve defined how to make an array of 2 objects in terms of an array of 1 object We’ve defined how to make an array of 1 object directly
[define horizontal-array [object spacing count →
[if [= count 1] object [group object [translate [point spacing 0]
[horizontal-array object spacing [− count 1]]]]]]]
![Page 4: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/4.jpg)
Recursion
Solving a problem using the solution to a simpler version of the problem
Recursive procedures Call themselves with “simpler” values for the
arguments For horizontal-array, this means count is smaller
Use the result to compute the final result Recursion needs to terminate
Can’t get lost infinite recursion (infinite regress) So before calling ourselves, we check to see if the
problem is so simple we can code the solution directly
![Page 5: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/5.jpg)
Schematic form of recursion
[define recursive [args … → [if simple-problem? simple-answer [fix-up [recursive simpler-args]]]]]
Every recursive procedure Checks for simpler version(s) of the problem Calls itself with a simpler version of the args
But some recursive procedures Have many checks for different simple versions of the problem Call themselves more than once (“tree recursion”) Just return the result of the recursive call without fix-up (“iteration”)
![Page 6: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/6.jpg)
Data as hierarchy
When we group objects inside groups inside other groups We get a tree-
structured hierarchy Kind of like outlines
And kind of like code
group
translatebox
group
translatebox
group
translatebox
![Page 7: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/7.jpg)
The snowflake fractal
A recursive shape Three sides formed as
follows: Start with a line Break the line into thirds Break the middle third and
stretch it Stretch until you have 4
equal pieces Now repeat the process on
each piece Keep going forever
![Page 8: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/8.jpg)
The snowflake in meta
How do we draw one of these “lines” Can’t really draw an infinite number of lines So we’ll only repeat the dividing process a finite
number of times Write a procedure that takes the number of
times to divide as an argument Draws a simple line if it’s zero (times to divide) Otherwise
Makes four copies of the line divided n-1 times (by recursing) Arranges them in the right places
![Page 9: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/9.jpg)
The with expression
[with name = exp …body]
Finds value of exp (once) Substitutes value of exp in for every occurrence
of name within body Evaluates body and returns its value Just a nice way of avoiding typing something
repeatedly
![Page 10: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/10.jpg)
The snowflake in meta
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
![Page 11: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/11.jpg)
First, the easy (non-recursive) case
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
![Page 12: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/12.jpg)
The hard case
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
![Page 13: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/13.jpg)
Segment 1
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
![Page 14: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/14.jpg)
Segment 2
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
shift 3 pixels and rotate 60°
![Page 15: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/15.jpg)
Segment 3 (very confusing, but trust me)
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
shift 3 more pixels
and rotate120°back
![Page 16: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/16.jpg)
Segment 4
[define snowflake-line[level → [if [= level 0] «End of recursion – just draw a line» [line [point 0 0] [point 9 0]] «Otherwise keep recursing» [with subline = [scale [/ 1 3]
[snowflake-line [- level 1]]] [group subline
[translate [point 3 0] [rotate -60 subline [translate [point 3 0]
[rotate 120 subline]]]] [translate [point 6 0] subline]]]]]]
(0,0) (3,0) (6,0)
shift 6 pixels from the original position
![Page 17: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/17.jpg)
Making the final snowflake
[define snowflake [level → [with line = [snowflake-line level] [rotate -60
line [translate [point 9 0] [rotate 120
line [translate [point 9 0]
[rotate 120 line]]]]]]]]
![Page 18: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/18.jpg)
Level 0 and 1 snowflakes
[snowflake 0] [snowflake 1]
![Page 19: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/19.jpg)
Level 2 and 3 snowflakes
![Page 20: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/20.jpg)
Level 4 and 5 snowflakes
Note: this has 3×45 = 3072 lines
![Page 21: Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a10460/html5/thumbnails/21.jpg)
Self-similarity:Zooming in on the level 5 snowflake