Nugroho's blog.: March 2009

Tuesday, March 17, 2009

Creating Tree with delphi

Now, I liked to explain how to do that, but firstly you have to open your Delphi.

Image below is result of code.


The code itself is like here
==================================
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons;

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
DrawGrid1: TDrawGrid;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
kotak: array[0..100,0..100]of integer;
node: array[0..100,0..100]of integer;
cabang: array[0..100,0..100]of integer;
anak: array[0..100,0..100]of integer;
jalan:boolean;
kemungkinan:integer;
implementation

{$R *.dfm}
procedure awal;
var i,j:integer;
begin
jalan:=false;
for i:=0 to 19 do begin
for j:=0 to 19 do begin
kotak[i,j]:=0;
node[i,j]:=random(kemungkinan);
anak[i,j]:=0;
form1.StringGrid1.Cells[j,i]:=inttostr(kotak[j,i]);
cabang[i,j]:=0;
//form1.DrawGrid1.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.Brush.Color:=claqua;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;end;
cabang[9,19]:=1;
node[9,19]:=1;
anak[9,19]:=1;
kotak[9,19]:=1;
form1.DrawGrid1.Canvas.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(9,19));
end;
procedure proses;
var i,j,k:integer;
begin
for i:=1 to 19 do begin
for j:=1 to 19 do begin
if cabang[i,j]=1 then begin
if node[i,j]>0 then begin
if random(kemungkinan)>0 then begin
kotak[i-1,j-1]:=1;cabang[i-1,j-1]:=1;anak[i-1,j-1]:=1;end;
//if random(2)=1 then begin
//kotak[i,j-1]:=1;cabang[i,j-1]:=1;anak[i,j-1]:=1;end;
if random(kemungkinan)>0 then begin
kotak[i+1,j-1]:=1;cabang[i+1,j-1]:=1;anak[i+1,j-1]:=1;end;
end;{node}
cabang[i,j]:=0;
end;{cabang}
if cabang[i,j]=0 then kotak[i,j]:=0;
if anak[i,j]=1 then kotak[i,j]:=1;
form1.StringGrid1.Cells[j,i]:=inttostr(kotak[j,i]);
{warna}
if kotak[i,j]=1 then begin
form1.DrawGrid1.Canvas.Brush.Color:=clgreen;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;
if kotak[i,j]=0 then begin
form1.DrawGrid1.Canvas.Brush.Color:=claqua;
form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,j));
end;
end;end;{for}
for i:=1 to 19 do begin
for j:=1 to 19 do begin
form1.DrawGrid1.Canvas.Brush.Color:=clwhite;
if kotak[i,j]=1 then begin
for k:=j+1 to 19 do begin
if kotak[i,k]=0 then form1.DrawGrid1.Canvas.FillRect(form1.DrawGrid1.CellRect(i,k));
end;{for}
end;{if}
end;end;{for}
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
awal;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
jalan:=true;
while jalan=true do begin
proses;
application.ProcessMessages;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
jalan:=false;
kemungkinan:=4;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
jalan:=false;
application.Terminate;
end;

end.
========================================
here is another image result, remember to always using randomize or your tree will always has same form in sequence.













323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)