Draw spiral

Draw spiral

Draw spiral

Simple spiral graphic generation tutorial. Demonstrate how to create mathematical procedure to create spirals on form.

DownloadDownload This Tutorials

Bookmark:

Draw spiral

This tutorial use one mathematical procedure calls SprialDraw to generate spiral. It uses some trigonometric functions for calculation and some rounding and truncations. To draw the spiral it uses canvas LineTo and MoveTo functions.

Draw spiral on form

Use following ButtonClick event to draw spiral.

procedure TForm1.Button1Click(Sender: TObject);
      
  Procedure SpiralDraw(Count:Integer);
  var
        cs, co, sn, si, cx, cy, x, y, ad, sf, xo: real;
        Counter:Integer;
        j, i: byte;
        xp, yp, color: Integer;
  begin
        cs := Cos(pi / 3);
        co := Cos(pi / 36);
        sn := Sin(pi / 3);
        si := Sin(pi / 36);
        cx := Round(Form1.Width/2);
        cy := Round(Form1.Height/2);
        ad := 1.16;
        sf := 1.06;
        Color:=20200;
        Counter:=0;
        xp := trunc(cx + 12 * ad);
        yp := trunc(cy + 0);
        Form1.Canvas.MoveTo(Xp,Yp);
        repeat
              Inc(Counter);
              x := 12;
              y := 0;
              For j := 0 to 70 do begin
                    For i := 0 to 6 do begin
                          Form1.Canvas.Pen.Color:=Color;
                          Color:=Color+1;
                          xp := trunc(cx + x * ad);
                          yp := trunc(cy + y);

                          If (i = 0) Then Form1.Canvas.MoveTo(xp, yp);
                          
                          Form1.Canvas.LineTo(xp, yp);
                          xo := x * cs - y * sn;
                          y := x * sn + y * cs;
                          x := xo;
                    End;
                    xo := sf * (x * co - y * si);
                    y := sf * (x * si + y * co);
                    x := xo;
              End;
        until Counter>Count;
  End;

begin
      SpiralDraw(500);
end;

Download This Delphi Tutorials.

Download materials for this article (Delphi - Tutorials)

Download - Draw-Spiral.zipDraw-Spiral.zip
       File size: 4 KB, File type: zip
       Total downloads: 292, Upload date: February 10 - 2009

Leave a comment