Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Make a swirl in Visual Basic .NET This example shows how to make a swirl in Visual Basic .NET. swirl, VB.NET Graphics, VB.NET

See Swirl by Eric W. Weisstein from MathWorld, a Wolfram Web Resource.

The program plots points in polar coordinates where the intensity of a point is given by:

`    F(r, t) = Sin(6 * Cos(r) - n * t)`

Subroutine DrawSwirl draws the fractal. For each pixel in the output area, the routine calculates the corresponding polar coordinates (r, t) and then plugs r and t into the equation above.

```Private Sub DrawSwirl()
Const SCALE As Single = 10

Dim n As Single
Try
n = Single.Parse(txtN.Text)
Catch ex As Exception
Exit Sub
End Try

m_Bm = New Bitmap(picCanvas.ClientSize.Width, _
picCanvas.ClientSize.Height)

Dim cx As Single = m_Bm.Width / 2
Dim cy As Single = m_Bm.Height / 2
Dim theta As Single
For y As Integer = 0 To m_Bm.Height - 1
Dim dy As Single = (y - cy) / SCALE
Dim dy2 As Single = dy * dy
For x As Integer = 0 To m_Bm.Width - 1
Dim dx As Single = (x - cx) / SCALE
Dim r As Single = Sqrt(dx * dx + dy2)
If dx = 0 AndAlso dy = 0 Then
theta = 0
Else
theta = Atan2(dy, dx)
End If
Dim f As Single = Sin(6 * Cos(r) - n * theta)
Dim b As Integer = 128 + 127 * f
m_Bm.SetPixel(x, y, Color.FromArgb(255, 0, 0, _
b))
Next x
Next y
End Sub```

The picCanvas control's Paint event handler displays the bitmap drawn by DrawSwirl.

```Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
As System.Windows.Forms.PaintEventArgs) Handles _
picCanvas.Paint
If m_Bm Is Nothing Then Exit Sub

e.Graphics.DrawImage(m_Bm, 0, 0)
End Sub```