unit Unit9; { This software has been designed and it is CopyLefted by Han de Bruijn: (===) @-O^O-@ #/_\# ### VORONOI REGIONS BY BRUTE FORCE CONTOURING ========================================= } INTERFACE uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Grafisch, Contours; 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} type punt = record x,y : double; end; vlak = array of punt; var nodes : integer; rij : vlak; procedure Some_Help; begin Writeln; Writeln('Voronoi regions contouring'); Writeln; writeln('Syntax: [program] [nodes]'); Writeln(' nodes = # random points'); Halt; end; procedure Read_Parameters; var woord : string; OK : boolean; k : integer; begin nodes := 10; if ParamCount > 1 then Some_Help; if ParamCount = 0 then begin Writeln('Default # nodes = 10'); Exit; end; 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 Some_Help; nodes := StrToInt(woord); end; 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; procedure tekenen(N : integer); { Make Drawing } var i,j,k,L : integer; x,y : double; OK : boolean; O : Omtrekken; bewaar : array of array of integer; begin { Make Random Sites } SetLength(rij,N); Form1.Image1.Canvas.Pen.Color := clBlack; Form1.Image1.Canvas.Pen.Width := 2; Form1.Image1.Canvas.Brush.Color := clWhite; for k := 0 to N-1 do begin rij[k].x := Random; rij[k].y := Random; i := x2i(rij[k].x); j := y2j(rij[k].y); Form1.Image1.Canvas.Ellipse(i-3,j-3,i+3,j+3); end; { Voronoi regions by Contouring } O := Omtrekken.Create; O.Afmetingen(Wijd,Hoog); SetLength(bewaar,Wijd,Hoog); O.Scherm := Form1.Image1.Picture.Bitmap; for j := 0 to Hoog-1 do begin y := j2y(j); for i := 0 to Wijd-1 do begin x := i2x(i); for k := 0 to N-1 do begin OK := Voronoi(k,x,y); if OK then bewaar[i,j] := k; end; end; end; for k := 0 to N-1 do begin for j := 0 to Hoog-1 do begin for i := 0 to Wijd-1 do begin O.funktie[i,j] := bewaar[i,j]-k-1/2 end; end; O.Rondom(false); for L := 1 to O.Aantal do begin O.Schetsen(L,clRed,2); end; end; end; procedure TForm1.Toetsdruk(Sender: TObject; var Key: Char); { On KeyPress } begin ClearDevice; Tekenen(nodes); Form1.Image1.Picture.SaveToFile('voronoi.bmp'); end; procedure TForm1.Scheppen(Sender: TObject); { At moment of creation } begin Read_Parameters; xmin := -0.1; xmax := 1.1; ymin := -0.1; ymax := 1.1; TV(Form1.Image1); ClearDevice; Tekenen(nodes); Form1.Image1.Picture.SaveToFile('voronoi.bmp'); end; END.