Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Building a Fractal Generator Go to Max Kleiner's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
A Fractal Library for Science, Chaos and Financials
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
03/29/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet Chaos Fractal Canvas Science Mandelbrot
Times Scored:
7
Visits:
3240
Uploader: Max Kleiner
Company: kleiner kommunikation
Reference: max.kleiner.com
Component Download: http://max.kleiner.com/download/chaoslib.pas
 
Question/Problem/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...


Zooming isn't as simple as it seems, cause zooming of fractals is
dependent on iterations not on graphic scales like form.widht/range or
so on.
the steps are as follow:

1. enlarge the picture with an increase of iterations

    cFrm:= vForm;
    X1:=20;
    X2:=trunc(cFrm.ClientWidth * zoomfact);
    .....

2. compute the enlargment
for i:= 10 to X2 do begin
    c1:= au;
    for j:= 0 to Y2 do begin
    .......

3. copy the section as you wish either by mouse or by position

cFrm.Canvas.CopyRect(cfrm.Canvas.ClipRect, cFrm.Canvas, SourceRect);

the point is the enlargment, cause it takes time to deepen the fractal
and my solution isn't efficient enough, cause it draws the fractal and
then grabs the clipping area, better is to define a structure or
collection, fill the data in an two dimensional array and then set the
area to draw.
AppRect:= Rect(cfrm.Left, cfrm.Top, cfrm.Left+ cfrm.Width,
                 cfrm.Top+ cfrm.Height);
GetCursorPos(Point);
//Check If The Mouse Pointer Is Outside Of The Mainform.
//If not PtInRect(AppRect,Point) Then
pointTL.X:= trunc((cfrm.Width - basew + 066)   * zoomfact);
pointTL.Y:= trunc((cfrm.height - baseh + 140) * zoomfact);
pointBR.X:= trunc((cfrm.width + 266) * zoomfact);
pointBR.Y:= trunc((cfrm.height + 233) * zoomfact);
SourceRect:= Rect(pointTL.x, pointTL.Y, pointBR.X, pointBR.Y);
cFrm.Canvas.CopyRect(cfrm.Canvas.ClipRect, cFrm.Canvas, SourceRect);
//InflateRect(sourceRect,Round(cfrm.Width/40),Round(cfrm.Height/40));






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Slight modification suggested.
    Richard Winston (Apr 15 2002 4:35PM)

To get this to run with Delphi 5, I had to change the uses statement in the implementation section to

{$IFDEF LINUX}
uses   QGraphics;      // cause of canvas.pen.color
  SysUtils, Classes, QForms ;
{$ELSE}
uses   Graphics;      // cause of canvas.pen.color
{$ENDIF}

Otherwise, it worked fine.  I expect to have some fun playing with it.
Respond

RE: Slight modification suggested.
Max Kleiner (Jul 5 2002 11:18AM)

thx, a lot of fun you get changing colors in:
cFrm.Canvas.pen.Color:= (16*count mod 255);
maybe by random
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
L. Rosenstein
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)