CHAPTER 14 ■ RECURSION
- Conversely, recursion is sometimes the best answer to a problem.
- Recursion can be an elegant solution to some problems, such as that of generating
the Fibonacci sequence. - Recursion is the underlying algorithm in making fractals.
I hope you enjoyed this chapter. It was one of my favorites to write, and I enjoyed creating sample
code for it. In fact, I played with many variations of the code, just for fun. You should do the same. In
particular, regarding the Sierpinski triangle program, try changing colors at every other depth or every
third depth value rather than at every depth value (hint: use the modulus operator). Then, play with the
values that control the appearance of the fractal tree. In particular, play with different numbers as inputs
to the randInt method. Also, rotate the tree and fiddle with the colors to create roots instead of the
branches of a tree.
I leave you with two challenges that will help you grow as a programmer. First, create a Sierpinski
gasket (a square of squares rather than a triangle of triangles). Second, create a Mandelbrot or Julia
fractal. I nearly included sample code for a Julia fractal (one of my favorites), but I thought it would be a
great exercise for you, now that you know the basics of recursion in Java. You'll need to do a bit of
research to find the algorithms and math behind the Sierpinski gasket and the Mandelbrot and Julia
fractals, but discovering algorithms and formulae is part of the job when you develop software. I'm sure
you'll get used to performing this kind of research.