unit Unit3; { 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} type punt = record x,y : double; end; vier = array[0..3] of punt; var keer : integer; function rechthoek(h : double) : vier; { Define Rectangle } var p : vier; begin p[0].x := -cos(h); p[0].y := -sin(h); p[1].x := +cos(h); p[1].y := -sin(h); p[2].x := -cos(h); p[2].y := +sin(h); p[3].x := +cos(h); p[3].y := +sin(h); rechthoek := p; end; procedure omgeving(p : vier); { Draw Circle and Rectangle } var k : integer; x,y : double; begin Form1.Image1.Canvas.MoveTo(x2i(1),y2j(0)); for k := 1 to 360 do begin x := cos(k/180*Pi); y := sin(k/180*Pi); Form1.Image1.Canvas.LineTo(x2i(x),y2j(y)); end; with Form1.Image1.Canvas do begin MoveTo(x2i(p[0].x),y2j(p[0].y)); LineTo(x2i(p[1].x),y2j(p[1].y)); LineTo(x2i(p[3].x),y2j(p[3].y)); LineTo(x2i(p[2].x),y2j(p[2].y)); LineTo(x2i(p[0].x),y2j(p[0].y)); end; end; procedure just_doit(p : vier); { Clip Lines against Rectangle } const hoe : array[0..3] of array[0..2] of integer = ((0,1,2),(1,0,3),(2,3,0),(3,2,1)); var g : array[0..3] of double; hoek : array[0..7] of boolean; boven,onder,links,rechts : boolean; h,x0,x1,y0,y1 : double; k,m,i,j : integer; begin h := Random*2*Pi; x0 := cos(h); y0 := sin(h); h := Random*2*Pi; x1 := cos(h); y1 := sin(h); with Form1.Image1.Canvas do begin Pen.Width := 1; MoveTo(x2i(x0),y2j(y0)); LineTo(x2i(x1),y2j(y1)); Pen.Width := 3; Pen.Color := clRed; end; for k := 0 to 3 do g[k] := (p[k].x-x0)*(y1-y0)-(p[k].y-y0)*(x1-x0); hoek[0] := (g[0] > 0) and (g[1] <= 0) and (g[2] <= 0) and (g[3] <= 0); hoek[1] := (g[0] <= 0) and (g[1] > 0) and (g[2] <= 0) and (g[3] <= 0); hoek[2] := (g[0] <= 0) and (g[1] <= 0) and (g[2] > 0) and (g[3] <= 0); hoek[3] := (g[0] <= 0) and (g[1] <= 0) and (g[2] <= 0) and (g[3] > 0) ; hoek[4] := (g[0] <= 0) and (g[1] > 0) and (g[2] > 0) and (g[3] > 0); hoek[5] := (g[0] > 0) and (g[1] <= 0) and (g[2] > 0) and (g[3] > 0); hoek[6] := (g[0] > 0) and (g[1] > 0) and (g[2] <= 0) and (g[3] > 0); hoek[7] := (g[0] > 0) and (g[1] > 0) and (g[2] > 0) and (g[3] <= 0); for m := 0 to 7 do begin if not hoek[m] then Continue; i := hoe[m mod 4][0]; j := hoe[m mod 4][1]; k := hoe[m mod 4][2]; x0 := (g[j]*p[i].x-g[i]*p[j].x)/(g[j]-g[i]); y0 := (g[k]*p[i].y-g[i]*p[k].y)/(g[k]-g[i]); Form1.Image1.Canvas.MoveTo(x2i(x0),y2j(p[i].y)); Form1.Image1.Canvas.LineTo(x2i(p[i].x),y2j(y0)); end; boven := (g[0] > 0) and (g[1] > 0) and (g[2] <= 0) and (g[3] <= 0); onder := (g[0] <= 0) and (g[1] <= 0) and (g[2] > 0) and (g[3] > 0); if boven or onder then begin y0 := (g[2]*p[0].y-g[0]*p[2].y)/(g[2]-g[0]); y1 := (g[3]*p[1].y-g[1]*p[3].y)/(g[3]-g[1]); Form1.Image1.Canvas.MoveTo(x2i(p[0].x),y2j(y0)); Form1.Image1.Canvas.LineTo(x2i(p[1].x),y2j(y1)); end; rechts := (g[0] > 0) and (g[1] <= 0) and (g[2] > 0) and (g[3] <= 0); links := (g[0] <= 0) and (g[1] > 0) and (g[2] <= 0) and (g[3] > 0); if links or rechts then begin x0 := (g[1]*p[0].x-g[0]*p[1].x)/(g[1]-g[0]); x1 := (g[3]*p[2].x-g[2]*p[3].x)/(g[3]-g[2]); Form1.Image1.Canvas.MoveTo(x2i(x0),y2j(p[0].y)); Form1.Image1.Canvas.LineTo(x2i(x1),y2j(p[2].y)); end; end; procedure TForm1.Toetsdruk(Sender: TObject; var Key: Char); { On KeyPress } var p : vier; begin ClearDevice; p := rechthoek(40/180*Pi); Form1.Image1.Canvas.Pen.Color := clBlack; Form1.Image1.Canvas.Pen.Width := 2; omgeving(p); just_doit(p); keer := keer + 1; Form1.Image1.Picture.SaveToFile('vb'+IntToStr(keer)+'.bmp'); end; procedure TForm1.Scheppen(Sender: TObject); { At moment of Creation } begin xmin := -1; xmax := +1; ymin := -1; ymax := +1; TV(Form1.Image1); keer := 0; end; END.