2004. augusztus 1., vasárnap

Building a Fractal Generator

Problem/Question/Abstract:

How do you build those bright, weird, beautiful shapes called fractals they're everywhere in science, art and forecasting with graphic-routines for Delphi and Kylix ?

Answer:

Fractals are geometric figures, just like rectangles, circles and squares, but fractals have special properties that those figures do not have.

There's lots of information on the Web about fractals, but most of it is either just pretty pictures or very high-level mathematics. So this article shows the important routine to draw the famous mandelbrot on a canvas.
Benoit Mandelbrot was largely responsible for the present interest in fractal geometry. He showed how fractals can occur in many different places in both mathematics and elsewhere in nature.

Much research in mathematics is currently being done all over the world. Although we need to study and learn more before we can understand most modern mathematics, there's a lot about fractals that we can understand.

Before we take a look at the mandelbrot-code a note about the unit:
A lot of programms does exists. I would only give you a glance at some code-snippets to motivate you, building your own generator with your own prameters in it. The OP-oriented library is free for download and shows some topics in Chaos like

Logistic Map
Henon
Lorenz Attractor
Bifurcation
Mandelbrot

So here's the mandelbrot(not a real universum picture just plain code ;)

procedure TModelMandelbrot.process(X, Y, au, bu: double;
X2, Y2: integer);
var
c1, c2, z1, z2, tmp: double;
i, j, count: integer;
begin
c2 := bu;
for i := 10 to X2 do
begin
c1 := au;
for j := 0 to Y2 do
begin
z1 := 0;
z2 := 0;
count := 0;
{count is deep of iteration of the mandelbrot set
if |z| >=2 then z is not a member of a mandelset}
while (((z1 * z1 + z2 * z2 < 4) and (count <= 90))) do
begin
tmp := z1;
z1 := z1 * z1 - z2 * z2 + c1;
z2 := 2 * tmp * z2 + c2;
inc(count);
end;
//the color-palette depends on TColor(n*count mod t)
cFrm.Canvas.pen.Color := (16 * count mod 255);
cFrm.Canvas.DrawPoint(j, i);
c1 := c1 + X;
end;
c2 := c2 + Y;
end;
end;

The different colors depends on the count which tells us the set of mandelbrot, those are different sets in and out so are different colors.
You call the unit simply by:

with TChaosBase(TModelMandelbrot.create) do
begin
setup(frmChaos); //aForm has to be set
Free;
end;

All models inherit from a baseclass:

TChaosBase = class
private
cFrm: TForm;
public
scaleX1: double;
scaleX2: double;
scaleY1: double;
scaleY2: double;
procedure setup(vform: TForm); virtual; abstract;
procedure scaleResults(const X, Y: double;
var intX, intY: integer;
width, height: integer);
end;

Thus, fractals graphically portray the notion of "worlds within worlds" which has obsessed Western culture from its tenth-century beginnings.
I hope you enjoy the magic world of fractals and maybe you earn some money on the stock-exchanges too, cause they belong to the same chaos-theorie...


Component Download: http://max.kleiner.com/download/chaoslib.pas http://max.kleiner.com/download/chaoslib.pas


Nincsenek megjegyzések:

Megjegyzés küldése