Programik w C#, który potrafi stworzyć zbiór Julii i Mandelbrota i zapisać wynik w pliku graficznym.
Można za jego pomocą przekonać się na czym polega różnica między tymi typami fraktali.
Świetna zabawa, polecam:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace JuliaAndMandelbrot
{
class Program
{
static void Main(string[] args)
{
// Tworzenie zbioru Julii
//Complex C = new Complex(-0.234, 0.332);
//JuliaSet Js = new JuliaSet();
//Area JuliaArea = new Area(-1.0, 1.0, -1.0, 1.0);
//List Set = Js.Create(C, 0.003, 1000, -1.0, 1.0, 300);
//SaveImage("Julia.png");
MandelbrotSet Ms = new MandelbrotSet();
Area ManArea = new Area(-2.0, 0.6, -1.0, 1.0);
Ms.Create(new Complex(0, 0), 0.003, 100, ManArea, 4);
SaveImage(Ms.Set, "Mandelbrot.png", 300);
}
static void SaveImage(List Set, string FileName, int Scale)
{
float MinX = 0, MaxX = 0, MinY = 0, MaxY = 0;
foreach (Complex c in Set)
{
MinX = (float)Math.Min(MinX, c.Re);
MaxX = (float)Math.Max(MaxX, c.Re);
MinY = (float)Math.Min(MinY, c.Im);
MaxY = (float)Math.Max(MaxY, c.Im);
}
int Width = Scale*Convert.ToInt32(MaxX - MinX);
int Height = Scale*Convert.ToInt32(MaxY - MinY);
Bitmap b = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(b);
g.Clear(Color.White);
Pen p = new Pen(Color.Black, 1);
float x, y;
foreach (Complex c in Set)
{
x = (float) ((c.Re / (MaxX - MinX)) * Width);
y = (float) ((c.Im / (MaxY )- MinY) * Height);
g.DrawLine(p, x, y, x + 1, y);
}
b.Save(FileName, ImageFormat.Png);
b.Dispose();
g.Dispose();
}
}
// Klasa pomocnicza
public class Area
{
public Area()
{
this.MinX = this.MaxX = this.MinY = this.MaxY = 0;
}
public Area(double MinX, double MaxX, double MinY, double MaxY)
{
this.MinX = MinX;
this.MaxX = MaxX;
this.MinY = MinY;
this.MaxY = MaxY;
}
public double MinX;
public double MaxX;
public double MinY;
public double MaxY;
}
public class FractalBase
{
protected int i;
protected List _Set;
public List Set
{
get { return this._Set; }
}
protected FractalBase()
{
this._Set = new List();
}
public virtual void Create(Complex C, double Delta, int Iterations, Area area, double Level)
{}
public void ToFile(string Path)
{
StreamWriter w = File.CreateText(Path);
foreach (Complex c in _Set)
w.WriteLine("{0};{1};", c.Re, c.Im);
w.Close();
}
} // end class
public class JuliaSet : FractalBase
{
private Complex _Z;
public JuliaSet() : base()
{
this._Z = new Complex();
}
public override void Create(Complex C, double Delta, int Iterations, Area area, double Level)
{
double real, imag;
for (real = area.MinX; real < area.MaxX; real += Delta)
for (imag = area.MinY; imag < area.MaxY; imag += Delta)
{
_Z.Re = real;
_Z.Im = imag;
for (i = 0; i < Iterations; i++)
{
_Z = _Z * _Z + C;
if (_Z.GetModulus() > Level)
{
_Set.Add(new Complex(real, imag));
break;
}
}
}
}
} // end class
public class MandelbrotSet : FractalBase
{
private Complex _C;
private Complex _Z;
public MandelbrotSet() : base()
{
_C = new Complex();
_Z = new Complex();
}
public override void Create(Complex Z, double Delta, int Iterations, Area area, double Level)
{
double real, imag;
for (real = area.MinX; real < area.MaxX; real += Delta)
for (imag = area.MinY; imag < area.MaxY; imag += Delta)
{
_C.Re = real;
_C.Im = imag;
_Z.Re = Z.Re;
_Z.Im = Z.Im;
for (i = 0; i < Iterations; i++)
{
_Z = _Z * _Z + _C;
if(_Z.GetModulus() > Level)
{
this._Set.Add(new Complex(real, imag));
break;
}
}
}
}
} // end class
}
Skorzystałem z klasy Complex autorstwa
Bena Houstona, którą znalazłem gdzieś na sieci
Complex.cs.zip
Przykładowe obrazki:
Zbiór Julii:
Żuczek Mandelbrota nieudany (błąd w programie):
I udany: