|
|
|
|
1
|
-
|
|
|
2
|
-#Recursion - Recursive Shapes
|
|
|
|
|
1
|
+# Recursion - Recursive Shapes
|
3
|
|
2
|
|
4
|
![main1.jpg](images/main1.jpg)
|
3
|
![main1.jpg](images/main1.jpg)
|
5
|
![main2.jpg](images/main2.jpg)
|
4
|
![main2.jpg](images/main2.jpg)
|
6
|
![main3.png](images/main3.png)
|
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
|
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.
|
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
|
|
26
|
|
28
|
3. Studied the concepts and instructions related to the laboratory session.
|
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
|
![figure2.png](images/figure2.png)
|
38
|
![figure2.png](images/figure2.png)
|
40
|
|
39
|
|
|
|
|
|
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
|
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:
|
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
|
`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$$.
|
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
|
|
85
|
|
87
|
---
|
86
|
---
|
88
|
|
87
|
|
89
|
-## Laboratory Session
|
|
|
|
|
88
|
+## Laboratory Session:
|
90
|
|
89
|
|
91
|
In today's laboratory experience you will implement recursive functions to produce fractals.
|
90
|
In today's laboratory experience you will implement recursive functions to produce fractals.
|
92
|
|
91
|
|
|
|
|
|
102
|
|
101
|
|
103
|
---
|
102
|
---
|
104
|
|
103
|
|
105
|
-#### Instructions:
|
|
|
|
|
104
|
+#### Instructions
|
106
|
|
105
|
|
107
|
1. Load the project `RecursiveShapes` into `QtCreator`. There are two ways to do this:
|
106
|
1. Load the project `RecursiveShapes` into `QtCreator`. There are two ways to do this:
|
108
|
|
107
|
|
|
|
|
|
114
|
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.
|
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
|
In this exercise, your task is to program a recursive function `boxes`, in the file `boxes.cpp`, that produces the following figures.
|
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
|
* `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**.
|
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
|
* `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`.
|
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
|
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.
|
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
|
|