(c) 2002 Visual Studio Magazine
Fawcette Technical Publications

February 2002
"Pick the Right Tool for the Job" by Bill Wagner

C#
Listing 1
Calculate the Mandelbrot Set. Give the MandelbrotGenerator class (x,y) points, and it returns a number that determines how far the point is from the proper set. You can plug this local component into an application to display the set.


namespace Mandelbrot
{
	using System;

	// <summary>
	// This class can generate a Mandelbrot set.
	// To use it, set the MaxIters property to 
	// some reasonable value; 100 is probably 
	// adequate. After that, call the index 
	// method with values for x and y. It 
	// returns the number of iterations before 
	// that point's value expands toward 
	// infinity.
	// </summary>
	public class MandelbrotGenerator
	{
		// The maximum and minimum number of 
		// iterations to try.
		private const int UpperBound = 500;
		private const int LowerBound = 10;
		// The number of iterations to try.
		private int maxIterations = 100;

		// <summary>
		// Maximum number of iterations 
		// property.
		// <remarks>
		// This property determines the number of 
		// iterations to use for the Mandelbrot 
		// equations; 100 is usually an adequate 
		// value, 250 is certainly safe. The more 
		// iterations you use, the longer the 
		// computations will take.
		// </remarks>
		// </summary>
		public int MaxIters
		{
			get
			{
				return maxIterations;
			}
			set
			{
				maxIterations = value;
				if (maxIterations > UpperBound)
					maxIterations = UpperBound;
				else if (maxIterations < LowerBound)
					maxIterations = LowerBound;
			}
		}

		// <summary>
		// Indexer to retrieve the Z value at a 
		// given point (x,y). This property 
		// calculates whether or not a given point
		// (x,y) is in the Mandelbrot set. The 
		// number of iterations before the point 
		// begins approaching infinity is 
		// returned.
		// </summary>
		public int this[double x, double y]
		{
			get 
			{
				return calcZ (x, y);
			}
		}

		// This function generates the number of 
		// iterations for a given point in the X,Y 
		// plane. Start with c=x+yi, where i is 
		// the sqrt (-1), and Z=0. Then, each new 
		// Z is the value of Z^2+c.
		private int calcZ(double xCoord, 
			double yCoord)
		{
			int iters = 0;
			double x = xCoord;
			double y = yCoord;
			while (iters < maxIterations)
			{
				double newx = x*x + xCoord - y*y;
				double newy = 2*x*y + yCoord;
				x = newx;
				y = newy;
				if ((Math.Abs(x) > 2.0) || 
					(Math.Abs(y) > 2.0))
				{
					// This point is outside the set. 
					// Done.
					break;
				}
				iters++;
			}
			return iters;
		}
	}
}

C#
Listing 2
Display the Mandelbrot Set. The MakeSet() function sets the bitmap colors using the values from the MandelbrotGenerator component. The application returns the value of z for each (x,y) pair in the bitmap. The colorizer uses the value of z to set the color of each pixel. 

private void MakeSet ()
{
	this.Cursor = Cursors.WaitCursor;
	double xCoord = minX;
	for (int x=0;x<xDim;x++, xCoord += scaleX)
	{
		double yCoord = minY;
		for (int y=0;y < yDim;y++, 
			yCoord += scaleY)
		{
			int thisValue = mandel[xCoord,yCoord];
			if (thisValue == 0)
				dispImage.SetPixel 
					(x,y,Color.White);
			else if (thisValue == mandel.MaxIters)
				dispImage.SetPixel 
					(x,y,Color.Black);
			else
				dispImage.SetPixel (x, y, 
					colorizor[thisValue]);
		}
	}
	this.Cursor = Cursors.Default;
}

C#
Listing 3
XML Object Returns Points in the Set. The mandelbrotPoint class provides the entry points so you can serialize a point in the Mandelbrot set as an XML node. The XML attributes must be public members of the class. The XMLRoot and XMLAttribute programming attributes inject the necessary code to support serializing the node in an XML document.

[XmlRoot("Point",Namespace="MandelbrotSet")]
public class mandelbrotPoint
{
	public mandelbrotPoint()
	{
		//
		// TODO: Add Constructor Logic here
		//
		// You need this, even though it does 
		// nothing. Otherwise, XML serialization 
		// won't work.
	}

	public mandelbrotPoint (double x, double y, 
		int z)
	{
		X = x;
		Y = y;
		Z = z;
	}

	[XmlAttribute("X")]
	public double X;
	
	[XmlAttribute("Y")]
	public double Y;
	
	[XmlAttribute("Z")]
	public int Z;
	}
}


C#
Listing 4
Return a Slice of the Set. The Slice method returns a slice of the set from a Web server. The MandelbrotGenerator returns the value of each point, and Slice initializes an array of mandelbrotPoint objects to pass back across the Web.

[WebMethod]
public mandelbrotPoint[] Slice (double minX, 
	double stepX, int numPts, double Y)
{
	mandelbrotPoint [] pts = new 
		mandelbrotPoint[numPts];
	int pt = 0;
	for (double x=minX; pt < numPts; 
		x+=stepX, pt++)
	{
		pts [pt] = new mandelbrotPoint (x, Y, 
			mandel[x, Y]);
	}
	return pts;
}
