Serge's World

Blogging about software development, astronomy, genealogy and more.

Fractals in C#: Julia Sets

Julia sets are very similar to Mandelbrot sets, and if you have not yet looked at my post on generating Mandelbrot sets, then I suggest you take a look. Most of the background is the same.

Julia Set

The only major difference is between Julia sets and the Mandelbrot set is that the Mandelbrot iteration is based on the equation Z = Z^(Power 1) + Z^(Power 2) + C, where C is the point Z(0). The Julia set is based on the equation Z^(Power 1) - Z^(Power 2) - L, where L is a constant complex number.

We iterate through each point, using the centre of the screen as the origin (0,0). For each point then, we iterate up to our maximum depth, using the Julia formula now instead of the Mandelbrot one, and break when the absolute value of Z reaches 2.

We can then draw the point, using the same method as we used for drawing the Mandelbrot point.

public void Generate(Graphics g, int iIterations, double Scaling, int iInitialSize, Complex L, double iOffsetRe, double iOffsetIm, int iLeft, int iTop, int iWidth, int iHeight, int iPower, int iPower2)
{
	Complex Z = new Complex( 0.0, -0.0);
	Complex Offset = new Complex( 0.0, -0.0);

	// iterate over the area in the complex plane indicated by the Scaling and Offset

	//   i is the real axis,  j is the complex axis


	for (double i = -iWidth/2; i < iWidth/2 ; i++)
	{
		for (double j = -iHeight/2; j < iHeight/2; j++)
		{
			// Normalize Z and adjust for Scaling and Offset

			Z.re = (i/((double)iInitialSize)) * Scaling + (double)iOffsetRe;
			Z.im = (j/((double)iInitialSize)) * Scaling + (double)iOffsetIm;

			// C is the Z(0) of the formula based on the pixel position

			// We've also added a ManualOffset from the text boxes on the screen


			int iteration = 0;

			for (int k = 1; k < iIterations; k++)
			{
				Z = (Z^iPower) - (Z^iPower2) - L;
				// if modulus of Z > 2, break out of the loop

				// and remember the current iteration to choose the color

				if (Z.Abs() > 2.0)
				{
					iteration = k;
					break;
				}
			}

			DrawComplexPoint(g, ((int)i) + (iWidth/2), ((int)j) + (iHeight/2), iteration);
		}
	}
}

The full source code is available at https://github.com/sjmeunier/fractalize.

Originally posted on my old blog, Smoky Cogs, on 30 Sep 2009

Tag Cloud

Algorithms (3) Android (10) Astronomy (25) Audio (1) Audiobooks (1) Barcodes (9) C# (69) Css (1) Deep sky (6) Esoteric languages (3) Family (3) Fractals (10) Gaming (1) Genealogy (14) General (2) Geodesy (3) Google (1) Graphics (3) Hubble (2) Humour (1) Image processing (23) Java (8) Javascript (5) jQuery (3) Jupiter (3) Maths (22) Moon (5) Music (4) Pets (5) Programming (88) Saturn (1) Science (1) Spitzer (4) Sun (4) Tutorials (68) Unity (3) Web (9) Whisky (13) Windows (1) Xamarin (2)