.comment-link {margin-left:.6em;}
Marc Boizeau's blog
Wednesday, November 24, 2004
  Mandelbrot in C#
Last week I talked a lot about Oracle and SQL, Mr Gates may be jealous ;-), let’s have a look to the .Net platform. Today I’m just going to introduce C# and a few stuff of the winform namespace (connection to Oracle will come later, be patient).

First of all, I work with Visual Studio .Net. If you want to understand something to this article you need this tool.

At school I learned Object Oriented Programming with C++ and Java. C# syntax is very close to those languages. The three languages have in common “{“, inheritance, encapsulation, a lot of keywords and something like a cultural background.
At school I also made a Mandelbrot set explorer in C++ as an OOP exercise. I find myself interesting to demonstrate C# coding with those fractal objects.

To do this I will present an object oriented construction to make calculus with complex numbers and the Mandelbrot specific method . I will then show how to use the Complex class into a simple WinForm to design Mandelbrot‘s set pictures.

OK, I won’t make a mathematical demonstration about Mandelbrot’s set here.
You just have to know that it deals with complex numbers and that you can make strange and sometimes beautiful pictures with it.

The idea is that when you multiply a complex number with itself and adds another complex number, this many times; it can grows up infinitely or converge to zero.
If it converges to zero it is a part of the set, when it grows up it is not. And you can measure at what “speed” it grows up.


So, here is my complex numbers class:

using System;// to include the basic libraries

namespace WindowsApplication1//a namespace groups classes working together,
{
// The main class
class Complex
{
//constants
const int IMAX_ITER = 255 ;// number of iterations to estimate the divergence
const double MODUL_LIMIT = 2;//


public double x;//pretty close to c++ syntax,
//"public" indicates that the users of the class can access this property...
public double y;

//////////////////////////
//constructors

//the basic one , does nothing but is mandatory for operators overloading
public Complex()
{}

//the main constructor
public Complex(double i, double j)
{
x = i;
y = j;
}

//////////////////////////
//Operators overloading, we define here basic operations on complex number

//minus
public static Complex operator -(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = (c1.x)-(c2.x);
temp.y = c1.y-c2.y;
return temp;
}
//plus
public static Complex operator +(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = (c1.x)+(c2.x);
temp.y = c1.y+c2.y;
return temp;
}
// multiplication, a little more complicated
public static Complex operator *(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x*c2.x-c1.y*c2.y;
temp.y = c1.y*c2.x+c1.x*c2.y;
return temp;
}

/////////////////////
//methods

//Module, used to measure the complex number
public double Module()
{
double temp;
temp = System.Math.Sqrt( x*x+y*y);
return temp ;
}
//use to measure the divergence of the complex number
//the number return is use to define the color of the affix's point
public int diverg()
{
int i=0 ;
Complex c = new Complex(x,y) ;

while (i<imax_iter&&c.Module()<MODUL_LIMIT)
{
c=c*c+this;//yes * and + are the operator we've just overload
}
return i;
}
}
}

So we have all the stuffs we need to decide if a complex number is a member of Mandelbrot’s set or not.
Now let’s use it in a Winform.
5 steps :

Step 1 In Visual Studio .net, create a new C# “Windows Application” project.
Step 2 Add a new object to the project (project window), a class(.cs file) names "complex". Then replace all the code of the file by the preceding complex class code.
Step 3 In the design view of the form1.cs add:
- A picture box named BoiteImage
- Four text boxes named respectively:
hgx(left and top X coordinate) default value:-0.5
hgy(left and top Y coordinate) default value:-1
bdx(right and bottom X coordinate) default value: 1.5
bdy (right and bottom Y coordinate) default value: 1
- A button named trace

If your not familiar with Visual studio: you will find all those stuff in the “tool box” window You can change the names and default values by right clicking + properties over the created objects. Then find the “name “ or “defaultvalue” properties in the “properties” window.

Step 4 In the project window double click on the ”trace” button. You are now in the Onclick event implementation method.
Put the following code into the “{ }”:

int nbPix=1000;
double diagDest;

int x,y;
Bitmap Imag;
Color col;
int iDiv;
Complex c= new Complex();

//define the complex plan zone
Complex cHG = new Complex(Convert.ToDouble( this.hgx.Text),Convert.ToDouble( this.hgy.Text));
Complex cBD = new Complex(Convert.ToDouble(this.bdx.Text),Convert.ToDouble(this.bdy.Text));
diagDest=Convert.ToDouble( this.hgx.Text)-Convert.ToDouble( this.bdx.Text);

//instantiate the bitmap
Imag = new Bitmap(nbPix ,nbPix);

//scan the bitmap
for(x=0;x= 255)
//if it converges it's black
col = System.Drawing.Color.FromArgb(255,0,0,0);
else //if it is not, it depends of the speed
col = System.Drawing.Color.FromArgb(128,iDiv,iDiv,iDiv);

//set the pixel color
Imag.SetPixel(x,y,col );
}
//draws the image
BoiteImage.SizeMode = PictureBoxSizeMode.StretchImage ;
BoiteImage.ClientSize = new Size(Imag.Size.Width, Imag.Size.Height);
BoiteImage.Image = (Image) Imag ;


Step 5 Now Hit F5 key and then click the « trace button » enjoy and change the range to explore the set!

That’s all folks
Sources and .exe on line soon !
Comments are welcome

Special thanks to my schoolmates Benoit and Jérôme .


 
Comments: Post a Comment



<< Home
You are a developer and work with Oracle and Microsoft technologies? Have a look!
ATOM
How to:
Use updatable views in Access
Get data in Excel from Oracle 1
Get data in Excel from Oracle 2
Draw the Mandelbrot set using C#
Use the "Grouping Sets" SQl Syntax
Use the "Rollup" SQl Syntax
Use the "Rank over" SQl Syntax

SQL Analytical : Rollup
Just another link today
A few Oracle and Microsoft links
Strange and usefull Oracle SQL structure
From the datawarehouse to the chart (part 2)
From the datawarehouse to the chart (part 1)
Using updatable views in Access
Introduction
ARCHIVES
October 2004 / November 2004 / December 2004 / January 2005 / February 2005 / March 2005 / April 2005 / June 2005 / August 2005 / September 2005 / December 2005 / February 2006 / December 2006 / March 2009 /


Powered by Blogger

mboizeau.free.fr