|
|
|
|
5
|
![main2.jpg](images/main2.jpg)
|
5
|
![main2.jpg](images/main2.jpg)
|
6
|
![main3.png](images/main3.png)
|
6
|
![main3.png](images/main3.png)
|
7
|
|
7
|
|
|
|
8
|
+[Verano 2016 - Ive]
|
|
|
9
|
+
|
8
|
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).
|
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
|
|
11
|
|
10
|
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.
|
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
|
|
13
|
|
12
|
-##Objectives:
|
|
|
|
|
14
|
+## Objectives:
|
13
|
|
15
|
|
14
|
1. Practice the definition and implementation of recursive functions.
|
16
|
1. Practice the definition and implementation of recursive functions.
|
15
|
|
17
|
|
16
|
|
18
|
|
17
|
|
19
|
|
18
|
-##Pre-Lab:
|
|
|
|
|
20
|
+## Pre-Lab:
|
19
|
|
21
|
|
20
|
Before coming to the laboratory session you should have:
|
22
|
Before coming to the laboratory session you should have:
|
21
|
|
23
|
|
|
|
|
|
25
|
|
27
|
|
26
|
3. Studied the concepts and instructions related to the laboratory session.
|
28
|
3. Studied the concepts and instructions related to the laboratory session.
|
27
|
|
29
|
|
28
|
-4. Taken the Pre-Lab quiz available through the course’s Moodle portal.
|
|
|
|
|
30
|
+4. Taken the Pre-Lab quiz available in Moodle.
|
29
|
|
31
|
|
30
|
---
|
32
|
---
|
31
|
|
33
|
|
|
|
|
|
84
|
|
86
|
|
85
|
---
|
87
|
---
|
86
|
|
88
|
|
87
|
-##Laboratory Session
|
|
|
|
|
89
|
+## Laboratory Session
|
88
|
|
90
|
|
89
|
In today's laboratory experience you will implement recursive functions to produce fractals.
|
91
|
In today's laboratory experience you will implement recursive functions to produce fractals.
|
90
|
|
92
|
|
91
|
-###Exercise 1: Snowflake
|
|
|
|
|
93
|
+### Exercise 1 - Snowflake
|
92
|
|
94
|
|
93
|
One of the simplest fractal figures is the snowflake. This figure is formed by an isosceles triangle, substituting the middle third segment on each side by an inverted "V". The measurements of each side of the "V" is equal to the measurements of the segment it substitutes. We will use the snowflake to illustrate the process of recursion.
|
95
|
One of the simplest fractal figures is the snowflake. This figure is formed by an isosceles triangle, substituting the middle third segment on each side by an inverted "V". The measurements of each side of the "V" is equal to the measurements of the segment it substitutes. We will use the snowflake to illustrate the process of recursion.
|
94
|
|
96
|
|
|
|
|
|
100
|
|
102
|
|
101
|
---
|
103
|
---
|
102
|
|
104
|
|
103
|
-####Instructions
|
|
|
|
|
105
|
+#### Instructions:
|
104
|
|
106
|
|
105
|
1. Load the project `RecursiveShapes` onto `QtCreator` by double clicking on the `RecursiveShapes.pro` file in the `Documents/eip/Recursion-RecursiveShapes` folder of your computer. You may also go to `http://bitbucket.org/eip-uprrp/recursion-recursiveshapes` to download the `Recursion-RecursiveShapes` folder to your computer.
|
107
|
1. Load the project `RecursiveShapes` onto `QtCreator` by double clicking on the `RecursiveShapes.pro` file in the `Documents/eip/Recursion-RecursiveShapes` folder of your computer. You may also go to `http://bitbucket.org/eip-uprrp/recursion-recursiveshapes` to download the `Recursion-RecursiveShapes` folder to your computer.
|
106
|
|
108
|
|
|
|
|
|
109
|
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.
|
111
|
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.
|
110
|
|
112
|
|
111
|
|
113
|
|
112
|
-###Exercise 2: Self-similar boxes
|
|
|
|
|
114
|
+### Exercise 2 - Self-similar boxes
|
113
|
|
115
|
|
114
|
In this exercise, your task is to program a recursive function `boxes`, in the file `boxes.cpp`, that produces the following figures.
|
116
|
In this exercise, your task is to program a recursive function `boxes`, in the file `boxes.cpp`, that produces the following figures.
|
115
|
|
117
|
|
|
|
|
|
127
|
* `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**.
|
129
|
* `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**.
|
128
|
* `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`.
|
130
|
* `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`.
|
129
|
|
131
|
|
130
|
-####Instructions
|
|
|
|
|
132
|
+#### Instructions:
|
131
|
|
133
|
|
132
|
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.
|
134
|
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.
|
133
|
|
135
|
|
|
|
|
|
143
|
|
145
|
|
144
|
---
|
146
|
---
|
145
|
|
147
|
|
146
|
-##Deliverables
|
|
|
|
|
148
|
+## Deliverables
|
147
|
|
149
|
|
148
|
1. Use "Deliverables" in Moodle to upload the `boxes.cpp` and `main.cpp` files. Remember to use good programming techniques, include the names of the programmers involved, and to document your program.
|
150
|
1. Use "Deliverables" in Moodle to upload the `boxes.cpp` and `main.cpp` files. Remember to use good programming techniques, include the names of the programmers involved, and to document your program.
|
149
|
|
151
|
|
|
|
|
|
152
|
|
154
|
|
153
|
---
|
155
|
---
|
154
|
|
156
|
|
155
|
-##References
|
|
|
|
|
157
|
+## References
|
156
|
|
158
|
|
157
|
[1] https://sites.google.com/a/wellesley.edu/wellesley-cs118-spring13/lectures-labs/lab-6-turtle-recursion.
|
159
|
[1] https://sites.google.com/a/wellesley.edu/wellesley-cs118-spring13/lectures-labs/lab-6-turtle-recursion.
|
158
|
|
160
|
|