Skip Navigation Links

Display Mandelbrot Set and Julia Set Fractals

This sample demonstrates how to plot the Mandelbrot Set and Julia Set fractals using the GraphicsForm object.

Run this sample with C# Programmable Calculator.

Julia Set

Mandelbrot Set

NOTE: To copy the following source code into C# Programmable Calculator, right-click the code and select the "View Source" or "View Page Source" menu. Then select the code, copy it, and then paste it into C# Programmable Calculator's code editor.

public struct Complex
{
  private double m_Real, m_Imaginary;
      
  public Complex(double Real, double Imaginary)
  {
    m_Real      = Real;
    m_Imaginary = Imaginary;
  }
      
  public double Size()
  {
    return m_Real * m_Real + m_Imaginary * m_Imaginary;
  }
      
  public double Real
  {
    get
    {
      return m_Real;
    }
      
    set
    {
      m_Real = value;
    }
  }
      
  public double Imaginary
  {
    get
    {
      return m_Imaginary;
    }
      
    set
    {
      m_Imaginary = value;
    }
  }
}
      
public abstract class Fractal
{
  public abstract double DistanceFromSet(int x, int y, double Gap, Complex Origin, out int nIterations, double MinimumDistance, int MaxIterations);
      
  protected double acorner;
  protected double bcorner;
  protected double side;
  protected String name;
      
  private class ColorMap
  {
    private const int nBasis  = 8;
    public const  int nColors = nBasis * nBasis * nBasis;
        
    static public readonly Color[] crColors;
      
    static ColorMap()
    {
      const int nFactor = 255;
      
      crColors = new Color[nColors];
      
      int nIndex = 0;
      
      for (int i = 0; i < nBasis; i++)
      {
        for (int j = 0; j < nBasis; j++)
        {
          for (int k = 0; k < nBasis; k++)
          {
            crColors[nIndex++] = Color.FromArgb(i * nFactor / (nBasis - 1), j * nFactor / (nBasis - 1), k * nFactor / (nBasis - 1));
          }
        }
      }
    }
  }
      
  public void Draw(int width)
  {
    using (Bitmap bitmap = new Bitmap(width, width))
    {
      Graphics g = Graphics.FromImage(bitmap);
              
      // Make background of bitmap white.
      g.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);
      
      const int    MaxIterations   = 100;
      const double MinimumDistance = 4.0;
              
      double Gap = side / width;
              
      Complex Origin = new Complex(acorner, bcorner);
              
      for (int x = 0; x < width; x++)
      {
        for (int y = 0; y < width; y++)
        {
          int nColor, nIterations;
                
          double Distance = DistanceFromSet(x, y, Gap, Origin, out nIterations, MinimumDistance, MaxIterations);
                  
          if (Distance <= MinimumDistance)
          {
            nColor = 0;
          }
          else
          {
            nColor = (int) (((double) (MaxIterations - nIterations) / (double) MaxIterations) * (double) (ColorMap.nColors - 1));
                    
            if (nColor < 0 || nColor > ColorMap.nColors - 1)
            {
              nColor = ColorMap.nColors - 1;
            }
          }
      
          g.DrawRectangle(new Pen(ColorMap.crColors[nColor]), x, y, 1, 1);
        }
               
      }
      
    // Save bitmap to .png file.
    string filename = Path.GetTempFileName() + ".png";
    
    bitmap.Save(filename);

    string html = string.Format("<HTML><BODY><IMG src=\"file:///{0}\"/></BODY></HTML>", filename);
      
    iGUIUtils.DisplayBrowser(name, html, true);
    }
  }
}
      
public class Mandelbrot : Fractal
{
  public Mandelbrot()
  {
    acorner = -2.0;
    bcorner = -1.25;
    side    = 2.5;
    name    = "Mandelbrot Set";
  }
      
  public override double DistanceFromSet(int x, int y, double Gap, Complex Origin, out int Iterations, double MinimumDistance, int MaxIterations)
  {
    double Size;
    Iterations = 0;
           
    // Convert x, y Cartesian point to a complex number.
    Complex ComplexPoint = new Complex(x * Gap + Origin.Real, y * Gap + Origin.Imaginary);
            
    Complex Zero = new Complex(0.0, 0.0);
            
    do
    {
      // Zero = Zero ^ 2 + ComplexPoint
      double Tmp = (Zero.Real + Zero.Real) * Zero.Imaginary + ComplexPoint.Imaginary;
             
      Zero.Real = (Zero.Real * Zero.Real - Zero.Imaginary * Zero.Imaginary + ComplexPoint.Real);
              
      Zero.Imaginary = Tmp;
              
      Size = Zero.Size();
    } while (Size < MinimumDistance && ++Iterations < MaxIterations);
            
    return Size;
  }
}
      
public class Julia : Fractal
{
  public Julia()
  {
    acorner = -1.5;
    bcorner = -1.5;
    side    =  3.0;
    name    = "Julia Set";
  }
      
  public override double DistanceFromSet(int x, int y, double Gap, Complex Origin, out int nIterations, double MinimumDistance, int MaxIterations)
  {
    double Size;
    nIterations = 0;
      
    Complex C = new Complex(-0.629578, -0.41524);
      
    // Convert x, y Cartesian point to a complex number.
    Complex ComplexPoint = new Complex(x * Gap + Origin.Real, y * Gap + Origin.Imaginary);
      
    Complex Zero = ComplexPoint;
      
    do
    {
      // Zero = Zero ^ 2 + ComplexPoint
      double Tmp = (Zero.Real + Zero.Real) * Zero.Imaginary + C.Imaginary;
          
      Zero.Real = (Zero.Real * Zero.Real - Zero.Imaginary * Zero.Imaginary + C.Real);
          
      Zero.Imaginary = Tmp;
          
      Size = Zero.Size();
    } while (Size < MinimumDistance && ++nIterations < MaxIterations);
        
    return Size;
  }
}
      
[Button("Samples", "Mandelbrot Set")]
public void DrawMandelbrotSet()
{
  Mandelbrot M = new Mandelbrot();
  M.Draw(500);
}
      
[Button("Samples", "Julia Set")]
public void DrawJuliaSet()
{
  Julia J = new Julia();
  J.Draw(500);
}


Home Page   Shareware   C# .NET Tips   Download C# .NET Source Code   Browse C# .NET Source Code   C# .NET Open Source   On-Line Utilities   Blog   Contact