(C)opyright 1987, Otto J. A. Smith (206) 385-1956 Wallpaper on the Amiga II This technique of 'fractalizing' formulas can produce pictures with an uncanny beauty. The easiest method of implementing these techniques is with the program Doug's Math Aquarium from Seven Seas Software. It is also possible to implement these algorithms by writing some fairly simple single purpose programs in BASIC or other languages that implement these same techniques. All of these techniques are based on mathematical templates. A mathematical template is not drawn on the screen but is generated mathematically. For example the expression (X*X + Y*Y) > 4 divides the plane into two parts. The expression evaluates to true for those parts of the plane outside of a circle with a radius of two, whose center is at the origin of the plane, and it evaluates to false for all parts of the plane within the circle. This expression can be used as a template to separate the plane and plot one set of values within the circle and another set without. For instance: IF (X*X + Y*Y) > 4 THEN COLOR = (X+Y*Y) MOD 32 ELSE COLOR = (X*X+Y) MOD 32 END IF In 'C' syntax: color = ( (x*x + y*y) > 4 ? (x + y*y) : (x*x + y) ) % 32 We have three formulas in our Wallpaper for the mind plot. (X*X + Y*Y) > 4 is our template. (X+Y*Y) MOD 32 is the formula for the wallpaper outside the circle, and (X*X+Y) MOD 32 is the formula for the wallpaper inside the circle. To observe how this template works, XMIN and YMIN should both be less than or equal to -2 and XMAX and YMAX should both be greater than or equal to 2. The meaning of XMIN, YMIN, XMAX and YMAX is given in part one of this paper. In the above formula the same colors are used to plot inside and outside the circle. It is also possible to separate the colors by changing the Modulo's and adding an offset. This is a fairly simple template. It divides the plane into only two parts. It is possible to make templates that are considerably more complex. For instance, in order to divide the plane into multiple concentric circles we could rewrite the formulas above as: IF ((X*X + Y*Y) MOD 8) > 3 THEN COLOR = (X+Y*Y) MOD 16 ELSE COLOR = ( (X*X+Y) MOD 16 ) + 16 END IF In 'C' syntax: color = ((x*x + y*y) % 8) > 3 ? (x + y*y) % 16 : ((x*x + y) % 16)+16 Several parameters are changed in this. First notice that we find X*X + Y*Y modulo 8. This effectively divides the whole plane into concentric circles. We also used > 3 instead of 4. This is really unnecessary, it just changes the relative width of the concentric circles. Also the modulo of the formulas is changed so that the bands will never share the same color registers. Again this is just because the author thought the picture looked better this way. The real feature to observe is how using the expression (f(x,y) MOD 8) > 3 divides the picture into multiple sub-pictures thereby creating a complex template. Here are the formulas for several different kinds of templates. (X mod 4) < 2 ; vertical bands two units wide. (Y mod 5) < 2 ; horizontal lines alternately 2 and 3 units wide. (X + Y) mod 2 = 1 ; Checker board 1 unit squares. SIN(X) + SIN(Y) > .5 ; Multiple dots with a diameter of pi units. Note that every template is just a condition that depends upon the values of x and y. In fact a template is just a function of x and y that returns either a true or a false value. Using templates and recursion, it is possible to build true fractals. It is easier to build other kinds of objects with many fractal characteristics, such as self similarity. For a good referance on fractals refer to Mandelbrots book, the Fractal Geometry of Nature. I call the recursive technique which I am about to describe, fractalizing a formula. This is not a highly accurate term. Many of the objects produced this way may not really be fractals, ie they may not have a fractal dimension that is fractional. I will start out with a semi-formal definition of fractilazation. If it is not clear, forge ahead. We will try to make it more transparent. Consider a function f(x,y), x and y are coordinates of a plane, and f is a function. For example f(x,y) might equal x*x + y*y or f(x,y) might equal sin(x+cos(y)) or in fact f(x,y) can be any function of x and y. Consider a second function called g(x,y) that is a template. In other words it returns either a value of true or false. We can say that the function f(x,y) has been fractalized by the template g(x,y) and by the two auxiliary functions m(x,y) and n(x,y) if we have combined all of these into a new function h(x,y) using the following structure. h(x,y) = IF g(x,y) THEN RETURN( f(x,y) ) ELSE RETURN( h(m(x,y),n(x,y)) ) END IF On the Amiga with 32 color registers we would then calculate COLOR = h(x,y) MOD 32 This is not really as bad as it sounds. Here is an example: g(x,y) = ((x*x + y*y) % 8) < 4 f(x,y) = (x*x + y*y)*5 m(x,y) = 1.7*sin(x)*x n(x,y) = 1.7*cos(y)*y The illustration is of this formula with XMIN = YMIN = 0 and with XMAX = 7 and with YMAX = 5. You can see the effect of the x*x + y*y in the large arcs. The effect of the sins and cosines is less apparent but can be seen in the squarish shapes with the rounded corners. What the above technique does is take a template and plot the formula f(x,y) in the true parts of the template. In the false parts of the template it changes the values of x and y using the functions m and n. This can distort both the template and the function. It can shrink or expand them, it can twist or otherwise distort them. It then replots using these new distorted values. This allows one to build pictures which, like the book that has its own picture on the cover, contains many pictures in pictures in pictures. It should be pointed out here that none of these recursive formulas are guaranteed to terminate, nor are they guarenteed to have defined values everywhere. It is very possible that your program will make an attempt to divide by zero or find the square root of a negative number or overflow in some other manner. One of the advantages of using Doug's Math Aquarium is that it takes care of all these things automatically and returns some sort of value at these points, and then continues on without blowing up and quitting. Another feature of Doug's Math Aquarium is that it will scale the result on request. This can eliminate aliasing and allow functions with small ranges (ie 0 to 1) to be scaled automatically without manually entering a multiplier and an offset. To express the Mandelbrot Set as an implementable fractalized formula requires that we extend the fractalization definition. The Mandelbrot set is actually defined as a set of pairs of x and y's for which a series based on these x and y's is bounded. All mandelbrot programs use techniques to estimate whether the series stays bounded. In this definition z is the number of iterations we calculate before we assume that the series is bounded. h(x,y,z) = H(x,y,x,y,z) H(a,b,x,y,z) = IF ( (x*x + y*y >= 4) or z=0 ) Then RETURN( z ) ELSE RETURN( H(a, b, (x*x - y*y + a), (2*x*y + b), z-1) ) END IF On the Amiga we would set COLOR = h(x,y,51) MOD 32 The value of z can be changed to improve the quality of the picture of the Mandelbrot set, but at the expense of taking more time to calculate. This is the standard method of reporting the results of the Mandelbrot caculation but it is not the only way. In Doug's Math Aquarium this definition takes three lines plus four entries for the initial XMIN, XMAX, YMIN and YMAX. Then the user can zoom in or away or change colors and scaling as desired. This is the code in terms of Doug,s Math Aquarium. val = fa3(x,y,51) fa = a3 ? fb4(a1*a1, a2*a2, a1*a2, a3-1) : 0 fb = a1+a2 < 4 ? fa3( a1-a2+x, a3+a3+y, a4) : a4 People familiar with 'C' will recognize the " ? : " sequence for the if-then construct. Arguements are numbered, which is the reason for the a1, a2 and a3. It is not really essential that anyone reading this article understand this notation as it is simply learned from the documentation for DMA, but it is nice to get a flavor for the DMA system and to see how easy it is to implement recursive formulas.