|
@@ -1,13 +1,12 @@
|
1
|
|
-
|
2
|
|
-#Recursion - Recursive Shapes
|
|
1
|
+# Recursion - Recursive Shapes
|
3
|
2
|
|
4
|
3
|
![main1.jpg](images/main1.jpg)
|
5
|
4
|
![main2.jpg](images/main2.jpg)
|
6
|
5
|
![main3.png](images/main3.png)
|
7
|
6
|
|
8
|
|
-[Verano 2016 - Ive]
|
|
7
|
+[Verano 2016 - Ive - Coralys]
|
9
|
8
|
|
10
|
|
-One commonly used programming technique is *recursion*. With this technique, problems are solved by solving similar problems but for smaller cases. We can construct sets of objects or tasks using *recursive rules* and *initial values*. *Recursive functions* are functions that are self-invoking, using smaller sets or elements each time, until reaching a point where an initial value is used instead of self-invoking. Fractals are an example of figures that can be created using recursion. In this laboratory experience you will practice the definition and implementation of recursive functions to draw self-similar objects (fractals).
|
|
9
|
+One commonly used programming technique is *recursion*. With this technique, problems are solved by solving similar problems, but for smaller cases. We can construct sets of objects or tasks using *recursive rules* and *initial values*. *Recursive functions* are functions that are self-invoking, using smaller sets or elements each time, until reaching a point where the initial value is used instead of self-invoking. Fractals are an example of figures that can be created using recursion. In this laboratory experience, you will practice the definition and implementation of recursive functions to draw self-similar objects (fractals).
|
11
|
10
|
|
12
|
11
|
The exercises in this laboratory experience are an adaptation of https://sites.google.com/a/wellesley.edu/wellesley-cs118-spring13/lectures-labs/lab-6-turtle-recursion.
|
13
|
12
|
|
|
@@ -27,14 +26,14 @@ Before coming to the laboratory session you should have:
|
27
|
26
|
|
28
|
27
|
3. Studied the concepts and instructions related to the laboratory session.
|
29
|
28
|
|
30
|
|
-4. Taken the Pre-Lab quiz available in Moodle.
|
|
29
|
+4. Taken the Pre-Lab quiz, available in Moodle.
|
31
|
30
|
|
32
|
31
|
---
|
33
|
32
|
|
34
|
33
|
---
|
35
|
34
|
|
36
|
35
|
|
37
|
|
-##Self-similar forms
|
|
36
|
+## Self-similar Forms
|
38
|
37
|
|
39
|
38
|
![figure2.png](images/figure2.png)
|
40
|
39
|
|
|
@@ -42,7 +41,7 @@ Before coming to the laboratory session you should have:
|
42
|
41
|
|
43
|
42
|
---
|
44
|
43
|
|
45
|
|
-One ingenious way of practicing and visualize recursion is programming functions that produce recursive figures, or fractals. For example, consider a recursive figure that we'll call *branch*. Figure 3 shows `branch(0,90)`, `branch(1,90)`, and `branch(2,90)`.
|
|
44
|
+One ingenious way of practicing and visualizing recursion is programming functions that produce recursive figures, or fractals. For example, consider a recursive figure that we'll call *branch*. Figure 3 shows `branch(0,90)`, `branch(1,90)`, and `branch(2,90)`.
|
46
|
45
|
|
47
|
46
|
---
|
48
|
47
|
|
|
@@ -55,7 +54,7 @@ One ingenious way of practicing and visualize recursion is programming functions
|
55
|
54
|
Can you see the recursive behavior in this figure? Notice that `branch(0,90)` is only a vertical segment (a segment in an angle of 90 degrees); `branch(1,90)` is `branch(0,90)` with two segments inclined in its top. More precisely, `branch(1,90)` is `branch(0,90)` with a `branch(0,60)` and a `branch(0,120)` in its top. Similarly, `branch(2,90)` is `branch(0,90)` with two `branch(1,90)` inclined in the top. That is, `branch(2,90)` is:
|
56
|
55
|
`branch(0,90)` with a `branch(1,60)` and a `branch(1,120)` in its top. Notice that $$60=90-30$$ and $$120=90+30$$.
|
57
|
56
|
|
58
|
|
-This way we can express `branch(n,A)` as a composition of branches with smaller inclined $$n$$'s. Code 1 provides a way of expressing `branch` as a recursive function.
|
|
57
|
+This way we can express `branch(n,A)` as a composition of branches with $$n$$ smaller and inclined branches. Code 1 provides a way of expressing `branch` as a recursive function.
|
59
|
58
|
|
60
|
59
|
---
|
61
|
60
|
|
|
@@ -86,7 +85,7 @@ Can you predict how the next iteration for the figure will look? That is, what f
|
86
|
85
|
|
87
|
86
|
---
|
88
|
87
|
|
89
|
|
-## Laboratory Session
|
|
88
|
+## Laboratory Session:
|
90
|
89
|
|
91
|
90
|
In today's laboratory experience you will implement recursive functions to produce fractals.
|
92
|
91
|
|
|
@@ -102,7 +101,7 @@ One of the simplest fractal figures is the snowflake. This figure is formed by a
|
102
|
101
|
|
103
|
102
|
---
|
104
|
103
|
|
105
|
|
-#### Instructions:
|
|
104
|
+#### Instructions
|
106
|
105
|
|
107
|
106
|
1. Load the project `RecursiveShapes` into `QtCreator`. There are two ways to do this:
|
108
|
107
|
|
|
@@ -114,7 +113,7 @@ One of the simplest fractal figures is the snowflake. This figure is formed by a
|
114
|
113
|
In the `main` function, look up the line where the variable `level` is declared and given a value. Change the value of `level` to `0` and run the program. You'll be able to see the triangle that represents the recursive base case for the snowflake. Continue changing the value for `level` and running the program so you can see the recursion process and produce self-similar figures.
|
115
|
114
|
|
116
|
115
|
|
117
|
|
-### Exercise 2 - Self-similar boxes
|
|
116
|
+### Exercise 2 - Self-similar Boxes
|
118
|
117
|
|
119
|
118
|
In this exercise, your task is to program a recursive function `boxes`, in the file `boxes.cpp`, that produces the following figures.
|
120
|
119
|
|
|
@@ -132,7 +131,7 @@ The `boxes` recursive function includes three parameters: `sideLength`, `shrinkF
|
132
|
131
|
* `shrinkFactor`: a real number that determines the rate of the next level of boxes. For example, if `sideLength` is `100`, and `shrinkFactor` is `0.3`, the length of the sides of the largest box will be `100` units, and the length of the sides of the smallest box will be `100*.3=30` units. Four copies of that smaller box are placed within the previous box, **one box in each corner**.
|
133
|
132
|
* `smallestLength`: is an integer that determines the length of the sides of the smallest box that will be drawn. For example, in Figure 6, `boxes(400,0.4,200)` only draws the box with sides of length `400`, since the size that will follow will be `400 * 0.4 = 160`, which is smaller than `200`. On the other hand, `boxes(400, 0.4, 75)` draws the box of size `400` and the boxes with size `160`, but not the following ones in size, since they would be of size `160 * 0.4 = 64`, which is less than `75`.
|
134
|
133
|
|
135
|
|
-#### Instructions:
|
|
134
|
+#### Instructions
|
136
|
135
|
|
137
|
136
|
1. Study the `box` function included in the `boxes.cpp` file. This function receives as arguments the coordinates of the upper left corner, the length of the sides and the color of the box. The function draws a box with these specifications.
|
138
|
137
|
|