Demo.pas

Top  Previous  Next

clip0001

 

 

 

unit Unit1;

 

interface

 

uses Windows,

    splines,

    Messages,

    SysUtils,

    Classes,

    Graphics,

    Controls,

    Forms,

    Dialogs,

    StdCtrls, ExtCtrls;

 

 

type TMainForm = class(TForm)

                Button1: TButton;

                procedure Button1Click(Sender: TObject);

   procedure FormCreate(Sender: TObject);

   procedure FormClose(Sender: TObject; var Action: TCloseAction);

                private { Private declarations }

                   procedure Draw;

                public   { Public declarations }

                Splines1: TSplines;

             end;

 

var MainForm: TMainForm;

 

implementation

 

{$R *.DFM}

 

procedure TMainForm.Draw;

var I,J     : Integer;

   BSpline : TBSpline;

   V       : TVertex;

begin

  {Wipe previous drawn splines from form}

  Canvas.Pen.Color:=clWhite;

  Canvas.Brush.Color:=clWhite;

  Canvas.Brush.Style:=bsSolid;

  Canvas.Rectangle(0,0,clientWidth,ClientHeight);

 

  {Then draw them all again.}

  for I:=1 to Splines1.NumberOfSplines do

  begin

     BSpline:=Splines1.GetSplineNr(I);

     if BSpline<>nil then

     begin

        For J:=0 to 200 do     {Draw the spline in 200 steps}

        begin

           V:=BSpline.Value(J/200);

           if J=0 then

           begin

              Canvas.Pen.Color:=Random(1003637);

              Canvas.MoveTo(Round(V.X),Round(V.Y));

           end else Canvas.LineTo(Round(V.X),Round(V.Y));

        end;

        For J:=1 to BSpline.NumberOfVertices do     {Draw the vertices}

        begin

           if BSpline.Interpolated then V:=BSpline.Value((J-1)/(BSpline.NumberOfVertices-1)) {If the spline is interpolated then calculate original vertices}

                                   else V:=BSpline.VertexNr(J);                              { else simply show the vertices}

           Canvas.Pen.Color:=clRed;

           Canvas.Ellipse(Round(V.X)-2,Round(V.Y)-2,Round(V.X)+2,Round(V.Y)+2);

        end;

     end;

  end;

  Splines1.Clear; {erase all splines from memory}

end;{TMainForm.Draw}

 

procedure TMainForm.Button1Click(Sender: TObject);

var BSpline : TBSpline;

   Vertex  : TVertex;

   I       : integer;

begin

  BSpline:=TBSpline.Create;

  randomize;

  for I:=1 to 10 do

  begin

    Vertex.X:=Random(ClientWidth);

    Vertex.Y:=Random(ClientHeight);

    Vertex.Z:=0; {Z coordinate is not used in this demo.}

    BSpline.AddVertex(Vertex);

 

  end;

  BSpline.Interpolate; { Simply remove brackets to interpolate the vertices}

  Splines1.Addspline(BSpline);

  Draw;

end;

 

procedure TMainForm.FormCreate(Sender: TObject);

begin

Splines1:= TSplines.Create(MainForm);

end;

 

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);

begin

Splines1.free;

end;

 

end.