CSE 131 Module 4: Encapsulation
Like all extensions and bridges, you can turn this in as you demo module 4,
OR you can demo it at midterm extension fest time or final extension fest time.
- This may be easier to undertake once you've seen module 5. Bridges are
exercises that span multiple modules, so you may want to hold off
on this until after module 5.
Bridge: Iteration and Encapsulation
Julia sets: fractal images
Fractals are mathematical models that
can create seemingly complex drawings from very simple specifications.
Because they are not easily bored, computers are well-suited to
You will use iteration in this lab to compute
a fractal drawing.
This lab emphasizes how computation can be applied
with relative ease and speed to create what
a human much longer to accomplish.
- Update your workspace and look for the julia package. To run
the program, run Main as a Java application.
- Please read over the entire document before you start. Really.
It will save you time in the long run.
- Take a look at the JavaDoc for this lab.
The JavaDoc for this lab is the main mechanism for communicating the
lab's design to you. You must implement the objects that are described
in the documentation, and JUNIT tests are provided to help you
determine if your code is working properly.
- If you are interested, see the
wallpaper a student made from
this lab. You can change constants and colors to get different patterns.
Fractals are an amazing mechanism for describing complex systems in terms
of simple and recursive components.
The idea of fractals in the complex plane (where the x coordinate is real and the y coordinate is imaginary)
dates back to 1918, when
Gaston Julia began research that characterized
fractals. In 1979,
Benoit Mandelbrot defined a map over Julia sets that reflected
the expense of computing a specific point in the set. Such maps
generate interesting patterns, and we will in a sense reproduce
Mandelbrot's work in this lab.
Since Mandelbrot's original work, computers have become faster and Java
has made programming them easier. Thus,
our renditions will be more aesthetically pleasing
(and won't take overnight to render either).
Google) to find some pages with more background information.
Problem statement: Drawing a Julia Set
This lab involves computation over some points of a complex plane.
Recall that a complex number has two components:
a real and an imaginary part.
For each complex point
c that we want to display, we compute
rigor(c) as follows.
Pseudocode for rigor(c):
In other words,
z = -0.7795 + 0.134 i
iters = 0
while abs(c) < 2 and iters < maxIters
c = c*c + z
iters = iters + 1
rigor(c) is the number of iterations
that it takes to compute the value at
The value of
maxIters is arbitrary, but let's assume
for now that
100 iterations are allowed for the computation. The function
abs(z) computes the distance of
the complex point (0,0).
To show complex numbers on an x-y axis system,
let us assume that the real component of a complex number is registered on
the x-axis; the imaginary component is thus registered on the y axis.
If we apply the above computation over the complex plane as x
ranges from -2 to 2 and y ranges from -2i to 2i,
we obtain the following picture:
| (-2, 2i)||(0, 2i)||(2, 2i)|
| (-2, 0i)
| (-2, -2i)||(0, -2i)||(2, -2i)|
|Figure 1: Initial display|
Note the complex coordinates of the upper-left and lower-right corners.
black areas represent points whose computation exceeded the limit set above (
In the figures shown in this write-up, the color of a pixel p is
computed as follows, based on the value of iters that was
computed for the Complex coordinate associated with p:
Color color = Color.black;
if (iters < maxIters)
color = Color.getHSBColor((iters % 256)/255.0f, 1.0f, 1.0f);
image.setPixel(i, j, color);
The above code uses the HSB color model. For more information,
consult the Color.getHSBColor documentation. The basic idea is to pick a hue based on the number of iterations,
but leave the brightness and saturation at full.
What is really interesting about a fractal drawing is that one
can dive into the drawing and discover ever increasing detail.
Below you see the results of zooming into the picture.
| (-1.6, -0.5i)
| (-1.6, 0.5i)
| (-1.51755, 0.063544i)
| (-1.51755, -0.0690i)
| Zoom in on left part of picture
|| Another zoom in on left part of picture
- Zooming into the picture changes the fractal plane coordinate of
the pixels. The ComplexRaster object keeps track of a
particular mapping between pixels and complex numbers.
The Julia object offers several ways to zoom, but the primary
method that you should consider is
- The zoom methods are called by doing one of the following:
- Mouse-drag within the displayed picture will cause a zoom into the
box described from the mouse press to the mouse release.
- The method menu offers ZoomIn and ZoomOut
- The reset method resets the picture to its original
You can save yourself time if you think about how to
reduce the above actions to calling a single method that
handles things generally, such as zoomTo.
Since this may be your first experience implementing code
from a design, you should first peruse the
documentation supplied for
- Work on the classes in the following order, testing as you go:
Your code for Julia must use the other two classes. You will not get
credit for this bridge if your code is sloppy! Use ComplexRaster in
Julia to manage the coordinate mapping from pixels to the complex plane.
- You are provided with JUnit tests for Complex and
Demo and submission
At a predesignated time (not during ordinary labs or studios), you
may demo this bridge to a TA. He or she will mark down the demo and
the SVN repo number of your julia package files.