Fern fractals are another class of iterative fractals, a type of iterative function system, that draws rather good images of ferns. And with a bit of tweaking of the input variables, the ferns of various sizes and shapes can be created using the same code. A Fern Fractal

To create a fractal fern you first need to supply a rather large amount of input variables that will control the shape of the fern. So in the application, the variables are stored in a few arrays.

```            double[] dA = new double;
double[] dB = new double;
double[] dC = new double;
double[] dD = new double;
int[] iRand = new int;

dA = 0;
dA = 0.16;
dB = 0.2;
dB = -0.26;
dB = 0;
dB = 0.23;
dB = 0.22;
dB = 1.6;
dC = -0.15;
dC = 0.28;
dC = 0;
dC = 0.26;
dC = 0.25;
dC = 0.44;
dD = 0.85;
dD = 0.04;
dD = 0;
dD = -0.04;
dD = 0.85;
dD = 1.6;
iRand = 1;
iRand = 8;
iRand = 15;
iRand = 85;
```

So, now we have arrays dA, dB, dC, dD and iRand. Very simply, the values in dA control the rachis of the fern (the main stem for those, like me, who weren’t listening in biology class), dB controls the left pinna (a “leaflet”), dC controls the right pinna, and dD controls the main shape of a frond (the fern’s “leaf”). iRand contains the probability that a value from each of the four arrays will be used.

Now the main function iterates through a specified number of points (which in the sample on this page, I used 100000 iterations), by first setting the current x and y coordinate to 0, and then it starts the iteration.

A random number is generated between 0 and 1, and if the number is less than iRand then it calculates the new xy coordinate based on the dA array. It then checks each probability through all the probabililities, each time calculating the xy coordinate using the array and calculation specified at that value.

Once the new coordinates are calculated, the point is drawn using the desired colour, and then the function loops to the next iteration, using these coordinates as the base coordinates for the next iteration.

Once the function has iterated enough times, the function exits, and you should be left with a decent fern.

Half the fun of this type of fractal is playing around with the input variables to see how they affect the output, so have fun. The full source code is available here.

```        public void GenerateFern(Graphics g, int iIterations, double dStartX, double dStartY, double dScale, double[] dA, double[] dB, double[] dC, double[] dD, int[] iRand, Color oColor)
{
FastRandom rnd = new FastRandom();
int iRandNum;
double dX = 0;
double dY = 0;
double dNewX = 0;
double dNewY = 0;

for (int i = 0; i < iIterations; i++)
{

iRandNum = rnd.Next(0, 100);
if (iRandNum < iRand)
{
dNewX = dA;
dNewY = dA * dY;

}
else if (iRandNum < iRand)
{
dNewX = (dB * dX) + (dB * dY) + dB;
dNewY = (dB * dX) + (dB * dY) + dB;
}
else if (iRandNum < iRand)
{
dNewX = (dC * dX) + (dC * dY) + dC;
dNewY = (dC * dX) + (dC * dY) + dC;
}
else
{
dNewX = (dD * dX) + (dD * dY) + dD;
dNewY = (dD * dX) + (dD * dY) + dD;
}
dX = dNewX;
dY = dNewY;
g.FillRectangle(new SolidBrush(oColor), (float)(dStartX + (dNewX * dScale)), (float)(dStartY - (dNewY * dScale)), 1, 1);
}

}
```