Due Date:
Work should be submitted via WISE Friday, March 19, 2010 before the beginning of class.
![]() |
Goals
|
String concatenation: Read about string concatenation in your text as well as the print() and println() commands in the online reference. Suppose you have the following items declared in Processing:
|
How would you print to the console the sentence "Frank is 21 years old" using the variables age
and
name
. Practice writing other thing that combine literal strings (e.g. "years old") with variables of
types string, int, float, boolean, etc.
Return values: Read about function return values in your text. For example, study the following code:
|
The add()
above is said to have a return value of type int
. Note, you could have simplied the setup()
function as follows:
|
Add additional functions for subtract()
and multiply()
. Test them out by printing out
sample results as was done above for add()
.
Once you have these functions, do you see that you can use the function return values to calculate more complex expressions such as c*(a+b) as follows:
|
Recursion: In class, we will go over how recursion works. Examine the examples in class and the example below.
|
Fibonacci Numbers: Read about the fibonacci sequence (see links above). The sequence can be computed recursively by noting that F(n) = F(n-1) + F(n-2). Write processing code that calculates the fibonacci sequence for some range of n. That is, complete the following code:
|
The program should output
|
Note, in the image, the colors have been added to help you differentiate circles of different sizes. Each radius has a specific color. There is 1 yellow circle, 2 green circles, 4 cyan circles, etc. Each level of recursion increases the number of circles by a power of 2.
![]() |
|
What happens if you change the 8 (int drawCircle) to a larger or smaller value? Try it.
What happens if you modify drawCircle by adding two additional lines:
|
Run the following Processing code. Study it carefully so you understand what it is doing. We will go over it in class but, to really understand it, you need to step through the code yourself.
|
Do the following:
Try changing the value of depth in setup() to see how the image changes. The values of depth should range from 0 to around 3. The resolution isn't high enough for anything much larger than 3.
In the above code, when depth is set to 1, one gets the image
![]() |
![]() |
![]() |
In the above code, when depth is set to 0, one gets an image that is just a horizontal line. Now, modify the goTurtle() function so that the starting shape (i.e. what you get when depth=0) is a square.
If you run the code with both of the changes you made in goTurtle() and recurse() then you should get the image:
When modifying recurse as in the above problem, it is important that:
Modify recurse() again but this time with any shape you want as long as it satisfies the above three criteria. It helps to draw the shape on a piece of paper in order to get the lengths and angles correct. You don't have to get the criteria exact, but try to come close.
Below is an example. Each segment is drawn with a different color so they are easier to see.
Now, relax the criteria, and see if you can come up with an interesting image. By relaxing the criteria, the entire shape will likely shrink or grow at each recursion level. It may also move off the screen altogether. You will just need to experiment. You can also combine it with color changes and loops or other things we have learned.
Friday, March 19, before class:
Turn in via Wise a single zipped file containing your Processing sketch for your code created in 4) and 5). Be sure to include image files of what your code produces.
Before turning in any of your code, make sure that you have cleaned it up!