Fractals, part III
Note: lab this week meets in the sun lab, in JGH 216. The code to get
into this lab will be given in class. The sun lab is a wonderful resource
with extensive hours. These machines are shared by the entire CS community
at DU, and this of course requires great respect and cooperation by a large
group of people. So far, it's working. As long as it continues to work,
the lab will be continue to be available to all at most times of the day and
night.
A PROBABILISTIC METHOD OF IMPLEMENTING ITERATED FUNCTION SYTEMS
The above description completely describes an iterated function system,
and you could implement the calculation of the final fractal image as
above. However, the transformation of entire images poses more
problems in implementing than transforming a single point.
Furthermore, the images converge very slowly to the attractor.
So we will use another method of approximating the
final fractal image. This method is easier to implement, and the image
converges quickly to a good approximation of the fractal.
In this method, instead of transforming the entire image at once,
we will transform a single point and repeat this process many times.
As part of this process, we will be choosing among the transformations at
random. So before we begin, we'll assign a probability to each of the
transformations. For example, in the leaning Sierpinski gasket, we could
say that the first transformation will be chosen 1/2 the time, and the other
two will each be chosen 1/4 of the time.
Begin with a randomly chosen point, call it (x,y). (You may also choose
a specific point, such as the origin.) Next select one of the
transformations according to the given probabilities. Transform the point
(x,y) according to that map to get (x1, y1). In other
words, x1 = ax+by +e and y1=cx+dy+f, where the constants
belong to the chosen transformation. Now take the point (x1,
y1) and transform it according to another randomly chosen
transformation, resulting in the point (x2,y2).
Continue in this way, producing a sequence of points.
Plot many many of these points - these points taken together give an
excellent approximation of the fractal.
Some technical issues
Using this method, we'll want to calculate the sequence of points using
double
s. But at some point, we need to store these points
in a 2-D array that represent our pixels. Each of our pixels will be
one byte, ranging from 0 to 255 (0 for black, 255 for white, values in-between
represent levels of gray). Thus, a variable to store a 100 by 100 pixel
image might look like this:
unsigned char image[100][100];
Remember that the first coordinate gives the number of rows (height) and
the second coordinate gives the number of columns (width).
Our last problem is to figure out where to draw each (x, y) point in
our 2-D image array. The image should hold our entire fractal. So before
we start, we'll need to know how big the fractal is and where it lies.
Calculate this by first
iterating images of points as already discussed, but instead of drawing them,
just keep track of what their boundaries are (max and min values for x and
y coordinates). Once you've done this you have xmin, xmax, ymin, ymax for
the fractal itself. Say that the 2-D array has width width and height
height. Now, to plot a point (x, y) on the 2-D array, we have
xpixel = (x-xmin)/(xmax-xmin)*width
ypixel = (ymax-y)/(ymax-ymin)*height
There's some asymmetry in these equations, because the y coordinate (the
row, that is) in our 2-D array starts from the top rather than the bottom.
So, we should plot the point (x, y) in image[ypixel][xpixel]
.
Another issue to deal with is how to save these images in files. We
will be using bitmap format (.bmp files). Bitmap file format will be covered
in lab this week.