Writing Your Own Functions
Prev |  | Next

## Writing Your Own Functions

When you get tired of the fractal functions which come with Gnofract 4D, you can write your own, or take advantage of thousands of formulas written by other fractal enthusiasts. Gnofract4D can load most fractal formula files written for Fractint (and some written for UltraFractal). However the compiler is not 100% backwards-compatible with Fractint, so unfortunately some fractals can't be loaded, or will display differently when they do. Gnofract 4D also supports many constructs Fractint doesn't, so you need to take extra care when writing formulas if you want them to work in Fractint too.

Here are links to some online resources for formula files:

- ORGFORM.ZIP A collection of about 25,000 Fractint formula files by many authors, originally compiled by George C. Martin and currently maintained by Paul N. Lee. Indispensable.

- UltraFractal public formula database Many thousands of formulas by users of UltraFractal. Most of these will work with Gnofract 4D. Let me know of any issues, since I aim to improve compatibility further in future releases.

### Writing Your First Formula

This section steps you through the creation of a new fractal formula. By the way, the formulas for each of these steps can also be found in the file `formulas/tutorial.frm`.

1. Create a new file called '`example.frm`' (the extension is important - Gnofract 4D uses this to decide whether the file is a formula or a coloring function).

2. Enter the following in `example.frm`.

       MyFormula {
       ; First example formula - this produces a variant on the Mandelbrot set
       init:
           z = 0
           c = #pixel
       loop:
           z = z*z*c + c*c
       bailout:
           |z| < 4.0
       }

3. Start Gnofract 4D, choose File | Open Formula File, and open example.frm. You should see MyFormula in the list of formulas to choose from. Select it and click Apply. You should see an image like this:

   A few things to note about the formula. It's divided into named sections, marked with a colon: "init", "loop". and "bailout". The compiler uses these to supply some of the standard scaffolding for a fractal function so you don't have to. The "loop" statement is the heart of the formula - this is the statement which is run repeatedly and which defines the shape of your fractal.

4. At this point, the widgets for rotating the image in 4D will be disabled, because your formula doesn't use any of the 4D options. Let's turn those on. Edit your formula so it reads:

       MyFormula {
       ; Second example - introduce 4D
       init:
           z = #zwpixel ; take initial value from 4D position
           c = #pixel
       loop:
           z = z*z*c + c*c
       bailout:
           |z| < 4.0
       }

   Then hit Refresh on the Formula Browser window. You should now find that all the options are enabled. This is because the image now depends on all 4 components of the 4D space, via #pixel and #zwpixel.

5. Next let's add some parameters to our function:

       MyFormula {
       ; Third example - add a parameter
       init:
           z = #zwpixel
           c = #pixel
       loop:
           z = @myfunc(z*z*c) + @factor * z + c*c
       bailout:
           |z| < 4
       default:
       param factor
       	default = (1.0,0.5)
       endparam
       }

   Hit Refresh again, then Edit | Fractal Settings to show the formula settings. You should two extra parameters in addition to the standard "Max Iterations" option: myfunc, with a drop-down list of functions, and fac (or Factor) with a draggable 4-way widget and 2 edit boxes. If you set myfunc to sqr and set factor to (-1,0.5) you should see:

   Parameters like this are a quick way to add more options to your fractal. Listing them in the "default" section is optional but provides a way to pre-populate your formula with values that work well. If you leave the default out Gnofract 4D will use "ident" for functions and 0 for numeric ones.

Prev |  | Next
About the maths | Home | Formula Language Reference