René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Writing a Mandelbrot AVI Movie with c#

This is an example of how to write an AVI movie with csharp. AviWriter.cs is needed to compile this example.

Downloading

MandelbrotMovie.zip contains

MandelbrotMovie.cs

MandelbrotMovie.cs
using System.Drawing;
using System;

class MandelbrotMovie {
  public static void Main() {
    try {
      Color[] palette = new Color[50];
      for (int i=0; i<50;i++) {
        palette[i]=Color.FromArgb(i*4,255-i*4,50+i*2);
      }
      int w = 600;
      int h = 600;
      AviWriter aw = new AviWriter();
      Bitmap bmp = aw.Open("test.avi", 25,w,h);

      double f                =  1.2;
      double centerX          = -0.7454333;
      double centerY          = -0.1130211;
      double pctAreaNewImage  =  0.9;
      double endWidth_times_2 =  0.0001;

      while (f>endWidth_times_2) {

        MandelBrot.CalcMandelBrot(
          bmp,
          centerX-f,centerY-f,
          centerX+f,centerY+f,
          palette);

        f=Math.Sqrt(pctAreaNewImage*f*f);
        aw.AddFrame();
        Console.Write(".");
      }
      
      aw.Close();
    }
    catch (AviWriter.AviException e) {
      Console.WriteLine("AVI Exception in: " + e.ToString());
    }
  }
}

Complex.cs

Complex.cs
using System;

public struct Complex {
  public Complex(double re_, double im_) {
    re = re_;
    im = im_;
  }
  public static Complex operator +(Complex arg1, Complex arg2) {
    return (new Complex(arg1.re + arg2.re, arg1.im + arg2.im));
  }

  public static Complex operator -(Complex arg1) {
    return (new Complex(-arg1.re, -arg1.im));
  }

  public static Complex operator -(Complex arg1, Complex arg2) {
   return (new Complex(arg1.re - arg2.re, arg1.im - arg2.im));
  }

  public static Complex operator *(Complex arg1, Complex arg2) {
    return (new Complex(
               arg1.re * arg2.re - arg1.im * arg2.im, 
               arg1.re * arg2.im + arg2.re * arg1.im));
  }

  public static Complex operator /(Complex arg1, Complex arg2) {
    double c1, c2, d;
    d = arg2.re * arg2.re + arg2.im * arg2.im;
    if (d == 0) {
      return (new Complex(0, 0));
    }
    c1 = arg1.re * arg2.re + arg1.im * arg2.im;
    c2 = arg1.im * arg2.re - arg1.re * arg2.im;
    return (new Complex(c1 / d, c2 / d));
  }

  public double Abs() {
    return (Math.Sqrt(re * re + im * im));
  }

  public double Arg() {
   double ret = 0;
   if (re != 0)
    ret = (180 / Math.PI) * Math.Atan(im / re);
    return (ret);
  }

  public override string ToString() {
    return (String.Format("Complex: ({0}, {1})", re, im));
  }

  private double re;
  private double im;
}

Mandelbrot.cs

Mandelbrot.cs
using System.Drawing;

class MandelBrot {
  static public void CalcMandelBrot(
                       Bitmap bmp, 
                       double xCoordFrom, double yCoordFrom, 
                       double xCoordTo,   double yCoordTo,
                       Color[] palette) {
    int width =bmp.Width;
    int height=bmp.Height;

    int iterMax = palette.Length;
    
    for (int x=0; x<width; x++) {
      double xCoord=xCoordFrom+(xCoordTo-xCoordFrom)/width*x;
      for (int y=0; y<height; y++) {
        double yCoord=yCoordFrom+(yCoordTo-yCoordFrom)/height*y;
        
        int cnt=0;
        
        Complex c = new Complex(xCoord,yCoord);
        Complex z = new Complex(     0,     0);
        Complex z2= new Complex(     0,     0); 
        while ((z2.Abs()<2.0) && (cnt < iterMax)) {
          z = z2 + c;
          z2=z*z;
          cnt++;
        }
        if (cnt == iterMax) {
          bmp.SetPixel(x,y,Color.FromArgb(0,0,0));
        }
        else {
          bmp.SetPixel(x,y,palette[cnt]);
        }
      }
    }
  }
}