unit Unit4; { This software has been designed and it is CopyLefted by Han de Bruijn: (===) @-O^O-@ #/_\# ### } INTERFACE uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Grafisch; type TForm1 = class(TForm) Image1: TImage; procedure Toetsdruk(Sender: TObject; var Key: Char); procedure Scheppen(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; IMPLEMENTATION {$R *.dfm} var veel : integer; procedure Stern_Brocot(getal : double); { Walk through Stern-Brocot tree } const dik : integer = 4; var m1,m2,n1,n2 : integer; OK : boolean; begin { Initialize tree } m1 := 0; n1 := 1; m2 := 1; n2 := 0; with Form1.Image1.Canvas do begin Brush.Color := clBlack; Pen.Width := 1; Pen.Color := clBlack; Ellipse(x2i(n2)-dik,y2j(m2)-dik,x2i(n2)+dik,y2j(m2)+dik); Ellipse(x2i(n1)-dik,y2j(m1)-dik,x2i(n1)+dik,y2j(m1)+dik) end; while true do begin OK := (m1 < veel) and (m2 < veel) and (n1 < veel) and (n2 < veel); if not OK then Break; if getal < (m1+m2)/(n1+n2) then { Tightening } begin { Upper Bound } m2 := m1+m2; n2 := n1+n2; with Form1.Image1.Canvas do Ellipse(x2i(n2)-dik,y2j(m2)-dik,x2i(n2)+dik,y2j(m2)+dik); end else begin { Lower Bound } m1 := m1+m2; n1 := n1+n2; with Form1.Image1.Canvas do Ellipse(x2i(n1)-dik,y2j(m1)-dik,x2i(n1)+dik,y2j(m1)+dik); end; end; end; procedure tekenen(r : double; wat : integer); var i,j : integer; x,y : double; begin with Form1.Image1.Canvas do begin Brush.Color := clWhite; Pen.Width := 1; Pen.Color := clBlack; end; for i := 0 to veel do begin for j := -1 to veel-1 do begin with Form1.Image1.Canvas do Ellipse(x2i(i)-2,y2j(j)-2,x2i(i)+2,y2j(j)+2); end; end; with Form1.Image1.Canvas do begin Pen.Width := 2; if wat = 1 then begin Pen.Color := clRed; y := -ln(r)/ln(3); MoveTo(x2i(0),y2j(y)); x := xmax; y := ln(2)/ln(3)*x - ln(r)/ln(3); LineTo(x2i(x),y2j(y)); end; Pen.Color := clYellow; MoveTo(x2i(0),y2j(ymin)); LineTo(x2i(0),y2j(ymax)); MoveTo(x2i(xmin),y2j(0)); LineTo(x2i(xmax),y2j(0)); Pen.Color := clBlue; MoveTo(x2i(0),y2j(0)); x := xmax; y := ln(2)/ln(3)*x; LineTo(x2i(x),y2j(y)); end; if wat = 0 then Stern_Brocot(ln(2)/ln(3)); end; procedure TForm1.Toetsdruk(Sender: TObject; var Key: Char); { Inactive } begin veel := 25; xmax := veel; ymax := veel-1; ClearDevice; tekenen(sqrt(2),1); Form1.Image1.Picture.SaveToFile('red_blue.bmp'); end; procedure TForm1.Scheppen(Sender: TObject); { At moment of creation } begin veel := 50; xmin := 0; xmax := veel; ymin := -1 ; ymax := veel-1; TV(Form1.Image1); ClearDevice; tekenen(sqrt(2),0); Form1.Image1.Picture.SaveToFile('stern_brocot.bmp'); end; END.