unit Unit1; { This software has been designed and it is CopyLefted by Han de Bruijn: (===) @-O^O-@ #/_\# ### Check, check, double check of DTFE method ----------------------------------------- } INTERFACE uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Algemeen, Shamos, Grafisch, Franco, Zijdelings, Sterren; 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 nodes : integer; procedure Some_Help; begin Writeln; writeln('Syntax: [program] [nodes]'); Writeln(' nodes = # random points'); Writeln(' Default # nodes = 10'); end; procedure Read_Parameters; var woord : string; OK : boolean; k : integer; begin nodes := 10; if not (ParamCount = 1) then Some_Help; if ParamCount = 0 then Exit; woord := ParamStr(1); OK := true; for k := 1 to Length(woord) do if not (woord[k] in ['0'..'9']) then OK := false; if not OK then begin Some_Help; Halt; end; nodes := StrToInt(woord); if nodes < 3 then begin Writeln('# nodes must be > 2'); Halt; end; end; procedure tellen(rij : punten; var uit : dubbel); { Pixel count of Voronoi regions } var i,j,k,N,min : integer; x,y : double; tel : integers; OK : boolean; function Voronoi(site : integer; x,y : double) : boolean; { Voronoi region that belongs to a (site) } var N,k : integer; x1,x2,y1,y2,xm,ym,f : double; OK : boolean; begin N := Length(rij); OK := true; x1 := rij[site].x; y1 := rij[site].y; for k := 0 to N-1 do begin if k = site then Continue; x2 := rij[k].x; y2 := rij[k].y; { For all Perpendicular Bisectors } xm := (x1+x2)/2; ym := (y1+y2)/2; f := (x-xm)*(x2-x1)+(y-ym)*(y2-y1); OK := OK and (f < 0); end; Voronoi := OK; end; begin N := Length(rij); SetLength(tel,N); for k := 0 to N-1 do begin tel[k] := 0; end; for j := 0 to Hoog-1 do begin y := j2y(j); for i := 0 to Wijd-1 do begin x := i2x(i); { Loop through all sites } for k := 0 to N-1 do begin OK := Voronoi(k,x,y); { Each Voronoi region pixel count } if OK then tel[k] := tel[k] + 1; end; end; end; { Doesn't always work } SetLength(uit,N); min := Wijd*Hoog; for k := 0 to N-1 do begin if tel[k] < min then min := tel[k]; uit[k] := 1/tel[k]; end; for k := 0 to N-1 do begin uit[k] := uit[k]*min; end; end; procedure teken_Voronoi(rij : punten; mesh : drietal); { Calculate & Draw Voronoi Regions } var L,F,i,j,k : integer; a,b,c,m,p : punt; buren : drietal; begin Form1.Image1.Canvas.Pen.Color := clGreen; Duaal(mesh,buren); F := Length(buren); for L := 0 to F-1 do begin a := rij[mesh[L].A]; b := rij[mesh[L].B]; c := rij[mesh[L].C]; omcirkel(a,b,c,m); i := buren[L].A; j := buren[L].B; k := buren[L].C; if not (i < 0 ) then begin Form1.Image1.Canvas.MoveTo(x2i(m.x),y2j(m.y)); a := rij[mesh[i].A]; b := rij[mesh[i].B]; c := rij[mesh[i].C]; omcirkel(a,b,c,p); Form1.Image1.Canvas.LineTo(x2i(p.x),y2j(p.y)); end; if not (j < 0 ) then begin Form1.Image1.Canvas.MoveTo(x2i(m.x),y2j(m.y)); a := rij[mesh[j].A]; b := rij[mesh[j].B]; c := rij[mesh[j].C]; omcirkel(a,b,c,p); Form1.Image1.Canvas.LineTo(x2i(p.x),y2j(p.y)); end; if not (k < 0 ) then begin Form1.Image1.Canvas.MoveTo(x2i(m.x),y2j(m.y)); a := rij[mesh[k].A]; b := rij[mesh[k].B]; c := rij[mesh[k].C]; omcirkel(a,b,c,p); Form1.Image1.Canvas.LineTo(x2i(p.x),y2j(p.y)); end; end; end; procedure test; { Common part } var k,N : integer; mesh : drietal; rij : punten; DTFE,uit : dubbel; begin ClearDevice; Opnieuw(nodes,rij); Delaunay(rij,mesh); Form1.Image1.Canvas.Pen.Color := clRed; teken_mesh(rij,mesh); Form1.Image1.Canvas.Pen.Color := clBlack; teken_extra(rij,mesh); teken_Voronoi(rij,mesh); tellen(rij,uit); histogram(rij,mesh,DTFE); N := Length(DTFE); { Equal for sensible regions } for k := 0 to N-1 do begin Writeln(k:3,DTFE[k],' = ',uit[k]); end; Writeln; Form1.Image1.Picture.SaveToFile('negen.bmp'); end; procedure TForm1.Toetsdruk(Sender: TObject; var Key: Char); { On KeyPress } begin test; end; procedure TForm1.Scheppen(Sender: TObject); { At moment of Creation } begin Tijdmeting(''); Read_Parameters; xmin := -0.1; xmax := 1.1; ymin := -0.1; ymax := 1.1; TV(Form1.Image1); test; end; END.