RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB
Expertise: Intermediate
Jan 2, 2007

How to Draw a Spiral in VB

Using a loop and the mathematical formula for a spiral, you can use pset(x,y) to plot the points returned by the formulas. The variables a and b determine the shape of the spiral. Generally, smaller numbers mean a tighter spiral. The following code shows an exponential spiral wherein the distance from the origin increases drastically with each rotation:

Option Explicit
Private Const Pi As Double = 3.14159265358979
Private Const e As Double = 2.718281828

Private Sub cmdDraw_Click()
    Dim cX As Long, X As Long
    Dim cy As Long, Y As Long
    'get the center of the form.
    cX = Me.ScaleWidth / 2
    cy = Me.ScaleHeight / 2
    a = 0.15
    b = 0.15
    'loop round to plot the points of the spiral.
    For i = 0 To 23040
        ang = (Pi / 720) * i
        X = cX + (a * (Cos(ang)) * (e ^ (b * ang)))
        Y = cy - (a * (Sin(ang)) * (e ^ (b * ang)))
        Me.PSet (X, Y)
    Next i
End Sub

Private Sub Form_Load()
    'set the form up.    
    Me.ScaleMode = vbPixels
    Me.AutoRedraw = True
    Me.WindowState = vbMaximized
End Sub
Richard Bunt
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date