Delphi Source Code for the Sierpinski Gasket

A brief explanation of the Delphi source code written to construct the Sierpinski Gasket. This is purely to show how it can be done, I wouldn't presume to claim that this is the best or only way as I'm sure it can be improved upon. Maybe given enough time I will revisit this code and make some changes but here it is for you to look at anyway. This is part of the Sierpinski program that is available for download on the downloads page or at the bottom of this page.

unit Sierpinski_Code;

interface

uses
    SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
    Forms, Dialogs, StdCtrls;

type
    TSierpinskiForm = class(TForm)
        CounterLabel: TLabel;
        StartButton: TButton;
        procedure FormCreate(Sender: TObject);
        procedure StartButtonClick(Sender: TObject);

    private
        { Private declarations }
    public
        { Public declarations }
    end;

        procedure Sierpinski;

var
    SierpinskiForm: TSierpinskiForm;
    total: longint;

implementation

{$R *.DFM}

procedure TSierpinskiForm.FormCreate(Sender: TObject);
begin
    total := 0;
end; {of TSierpinskiForm.FormCreate}

procedure Sierpinski;
var
    i, count, x, y: integer;
    xcoord, ycoord: array[1..3] of integer;
begin
    xcoord[1] := 275;
    ycoord[1] := 410;
    xcoord[2] := 40;
    ycoord[2] := 40;
    xcoord[3] := 510;
    ycoord[3] := 40;
    x := 40;
    y := 40;
    count := 0;
    total := total + 1;
    while count < 10000 do
    begin
        i := random(3) + 1;
        x := (x + xcoord[i]) div 2;
        y := (y + ycoord[i]) div 2;
        case i of
            1: SierpinskiForm.canvas.pixels[x,y] := clred;
            2: SierpinskiForm.canvas.pixels[x,y] := clgreen;
            3: SierpinskiForm.canvas.pixels[x,y] := clblue;
        end; {of case}
        count := count + 1;
        SierpinskiForm.CounterLabel.caption := inttostr(10000 * total);
    end; {of while}
end; {of sierpinski}

procedure TSierpinskiForm.StartButtonClick(Sender: TObject);
begin
    Sierpinski;
end; {of TSierpinskiForm.StartButtonClick}

end. {of program}

The above code was part of a program I wrote (available for download below) that had a 550 x 450 Form with a Start Button and a Label to display the number of points plotted. 

So onto the analysis of the main sections of the code. We have three user written procedures (all of the code above the line {$R *.DFM} is computer created and dependent on the Delphi Form and components presents with the exceptions of the variable total and the name of the Sierpinski procedure). I don't want to go into too much detail about the differences between Delphi code and other languages as I want to be mainly concerned about the mathematics behind the Sierpinski Gasket. So briefly the TSierpinskiForm.FormCreate procedure makes sure that the total variable is set to zero as the form is created and the TSierpinskiForm.StartButtonClick procedure simply calls the main Sierpinski procedure.

The Sierpinski procedure starts by creating six local variables which are used in addition to the global variable: total. I then create the three points of the triangle (A, B and C on the previous page). I have used variables that look like Cartesian co-ordinates because that's what I feel most comfortable with. So the three points are (xcoord[1], ycoord[1]), (xcoord[2], ycoord[2]) and (xcoord[3], ycoord[3]) or on screen this relates to (275, 410), (40, 40) and (510, 40). These points are constant (the triangle doesn't change shape or size) and so I then create the point that will be tracked and coloured accordingly. This is (x, y) and initially it starts at one of the points. It doesn't matter which one so I chose (40, 40) shown by the lines x:= 40; y := 40; Looking at the pictures on my mathematics page you will see that the shape doesn't start to materialise until the number of plots numbers in the thousands so instead of calling the procedure to plot a single point I wanted it to plot thousands of points per call and to tell me exactly how many points had been plotted in total. Therefore I have a local variable called count which monitors how many points have been plotted in this call and a global variable called total which monitors the total number of times that the procedure has been run. The next few lines are the main bits of the program. A random number is generated between 1 and 3 which corresponds to one of the triangle vertices then the program simply looks at where the last dot drawn was (x, y), halves the distance from it to the chosen vertex ((x + xp) / 2, (y + yp) / 2) and then colours it depending on which vertex was chosen.

If you would like to see this program in action you can download it here: Sierpinski_1_01.zip (92k) version 1.01 by S. Kennedy. Again, it's a program that was written just to prove to myself that I could do it so it isn't as good as it could be but take a look and maybe if I get time I will update it again. This program also generates the Sierpinski Carpet which is similar to the Gasket but based on squares not triangles.

Back to the Mathematics page