University of North Carolina at Asheville

CSCI 273: Mathematical Programming

Lab 10

Recursive Graphics

[Introduction] [Instructions] [What To Submit]


In this lab you will explore ways to produce complex images using simple recursive algorithms. Most of your tasks are based directly on demo programs posted on the Booksite for your text. A few problems may require a bit of insight and creativity on your part. But rejoice, your work in this lab should reward you with some (almost) instant visual gratification.


To complete this project, follow the steps listed below:

  1. Login to your home directory and launch a terminal window. Use the cd command to change your default directory to csci/273.002/labs. Keep this window open for future reference.
  2. Launch the NetBeans IDE and raise the New Project dialog. Define the project to be a Java Application named Lab10. Make sure that the project folder will be created within your csci/273.002/labs folder. As in your previous labs, you should also uncheck the Create Main Class checkbox before you click Finish.
  3. All of the programs you will be downloading or creating for this lab are clients of library classes that you gathered and archived into the JAR file Booksite.jar as part of Lab 08. To make all those library classes available to your Lab10 project, you should now download the equivalent archive Booksite.jar into csci/273.002/labs now. Then repeat the same procedure in the NetBeans IDE that you first performed as Step 6 in Lab 08.

  4. Your first exercise in this lab is actually just a repeat of the last task you performed in Lab 09, namely to download the plain-text version of (Program 2.3.4 in your text) from the Booksite into the default directory for your Lab10 project. This time your objective is not to build and run Htree again, but just to make its contents available as a starting point for creating a new program with a very similar framework...
  5. So now create a new Empty Java File named MySquares in the default directory, and start to fill it in by copying all the contents of After you change the class name in the source code to MySquares, adapt the framework of the Htree code to write a program that can draw either of the two recursive squares shown below. Note that these are just two of the four recursive squares specified by Creative Exercise 2.3.22 in your text. Incidentally, you can find this same problem listed as Exercise 2.3.19 (!) in Section 2.3 on the Booksite.

    Let your MySquares program accept two command-line arguments:

    Now Build the project and run MySquares from the terminal window. When everything is working, show your results to your lab instructor.

  6. To start your next task in this lab, find the demo (Program 2.3.5 in your text) on the Booksite and download it into the default directory for Lab10. To get some idea of what this program is all about, take a moment now to consult either Section 2.3 of your text (see pp. 270-272) or the equivalent Booksite discussion under Section 2.3. If the explanation does not quickly clarify everything for you, feel free to ask your lab instructor for help before you proceed...

    When you think you understand what this program is supposed to do, open in the NetBeans editor and look over the source code. Your goal at this stage is to see if you can understand just how this program performs its task. And again, get help from your lab instructor as necessary.

    Then Build your project again, and run this program using various values of its single command-line argument (the so-called Hurst exponent H).

  7. As you should understand by now, the demo is designed to illustrate the notions of one-dimensional fractional Brownian motion and Brownian bridges. Your next task is to create a new, enhanced version of this demo that might give you further insight into these concepts. You will want to leave the original version intact, to serve as a handy reference as you attempt your revisions. So start by creating a new copy of this demo, called, in the default directory for Lab10.

    The first change you want to make is based on the fact that Brownian uses recursion to break up an initially straight, horizontal line into 2n segments, where n is the order of the recursion. However, the recursive method curve() as defined in Brownian uses a base case that tests for a minimum segment length rather than the order n. This motivates your first task: revise so that it accepts the recursive order n as an additional command-line argument, and explicitly uses the value n = 0 to serve as its base case.

    As your second task, have the program accept the initial variance var, aka the volatility, as a third command-line argument. This should let you confirm that the volatility affects the overall scale of the vertical displacements, whereas the Hurst exponent H affects the smoothness of the curves.

    When these changes are in place, Build your Lab10 project again and run MyBrownian. If you initially fix var = 0.01 and H = 0.0 and make successive runs with n = 1 and n = 2, you should get images roughly like the ones shown below:

    If you now get brave and let n = 10, you should see something more like the leftmost image below. But if you leave n = 10 and let H = 0.5, the image should resemble the one on the right:

    Note that this last image, where the Hurst exponent H = 0.5, is known as a Brownian bridge.

    Now take a moment to try various other combinations of the three command-line arguments, and see if you understand how they affect the curves. Then show your results to your lab instructor.

  8. For your last task in this lab, return yet again to the Booksite and download the demo, which is posted as part of Web Exercise 45 near the end of Section 2.3. While you are at it, you might want to read the short explanatory paragraph that immediately precedes the problem statement.

    As with, you first want to understand the purpose of Infortunately, in this case neither the text (see page 272) nor the Booksite offers anything more than a sketchy discussion. You should actually get more insight by opening in the NetBeans editor and reading the comments as well as the source code itself. But of course, as a last resort you can always ask your lab instructor to make everything clear...

    You should next run this demo a few times to get some idea of its behavior. While you are doing this, you should examine the role of the single command-line argument accepted by the current version of this program.

    As you may have guessed by now, your real task here is to create a new, enhanced version of that might give you further insight into its behavior. You will again want to leave the original version intact for reference purposes, so first create a new copy of this demo, called (what else?)

    By this time you should realize that PlasmaCloud starts by assigning random colors (using a variant of the HSV color model) to the four corners of a unit square, and painting the entire square with an average of those four colors, or more precisely an average with an added random Gaussian fluctuation. Then it calls a recursive method that initially divides the square into four quadrants (or subsquares), and paints each quadrant with a similar average of four colors it derives for each of its corners. (These colors are themselves derived by averaging over the corner colors of the original unit square in fairly obvious ways.) And as always with recursive methods, things may not stop there...

    By now you may have realized that applying this recursive process n times will break up the original unit square into 4n segments. However, the Booksite version of is like, in that it uses a base case that specifies a minimum size of the squares, rather than the minimum recursive order n. So once again your first task is to revise so that it accepts n as a command-line argument, and explicitly uses the value n = 0 to serve as its base case.

    To make your second revision, locate the declaration of the variable stddev in the main() method. This quantity determines the scale of the random fluctuations in the color-averaging procedure. In the original PlasmaCloud, the value of stddev is simply assigned the value 1.0. However, the value of stddev strongly affects these images, and to understand its role here you want to convert it into a second command-line argument.

    Finally, you may soon find it helpful for this program to produce smaller windows than normal. So you could optionally take care of that in main() now. But for the moment you may just want to remember that StdDraw provides this option if you need it...

    After you make these changes, Build your Lab10 project yet again and run MyPlasmaCloud. Start out by keeping stddev = 0.0 for the moment (no random fluctuations at all), and try successive runs with n = 1, n = 2, n = 3, and n = 4. Remember that your local colors will vary, but with luck you should see something like the following sequence of images:

    You may soon discover that things slow down fast as n increases, so you won't want to go too far with these tests. But for n = 8 and stddev = 0.0, your window might look like the leftmost image below. Of course the colors in this image change quite smoothly, since no random fluctuations have been added in the averaging procedure. However, if you keep n = 8 but now let stddev = 1.0 you might get an image more like the one on the right:

    So as time permits, feel free to experiment with various values of stddev, and perhaps find other parameters to adjust in this program. But you will be officially done as soon as you can demonstrate your working version of MyPlasmaCloud to your lab instructor.

What To Submit

When your project is complete and working correctly, demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab10 project. Finally, before you logout, switch back to your terminal and set your default directory back to csci/273.002/labs. Then create a JAR file of your Lab10 project folder, using the command

jar cf Lab10Project.jar Lab10
Please leave both your Lab10 project folder and Lab10Project.jar in your csci/273.002/labs directory for the remainder of the semester.