# How to draw a spiral?

How to draw a spiral in a canvas?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Any particular kind of spiral in mind?

GL
Mike
0
Commented:
The easiest way to draw the spiral is using parametric equations. Basic spiral equation is (assuming t is the base parameter:

x(t) = r(t).cos(t)
y(t) = r(t).sin(t)

so:

procedure DrawSpiral(C : TCanvas; Turns: integer);
const
ox = 200;
oy = 200;
M_PI = 3.14159265358;
var
r : single;
t : single;
function x(r, t:single):single;
begin
Result := r*cos(t);
end;
function y(r, t:single):single;
begin
Result := r*sin(t);
end;
begin
t := 0; C.MoveTo(ox,oy);
r := 0;
while t < Turns*M_PI/2 do //draw a spiral total of 720 degrees of rotation
begin
C.LineTo(ox+Round(x(r,t)),oy-Round(y(r,t)));
t := t+0.2;
r := r+0.1;
end;
end;

procedure TExImageForm.pboxPaint(Sender: TObject);
begin
DrawSpiral(pbox.Canvas, 32);
end;

regards, igor
0

Experts Exchange Solution brought to you by