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

Title Draw a butterfly curve This example shows how to draw a butterfly curve in Visual Basic 6. graphics, curve, butterfly, butterfly curve Graphics

This program uses the following equations to draw the butterfly curve:

```    x = Cos(t) * Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5
y = Sin(t) * Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5```

Subroutine DrawCurve loops variable t through the values 0 to 24 * Pi to generate and connect the curve's points.

```Private Sub DrawCurve()
Const PI As Double = 3.14159265
Const NUM_LINES As Long = 5000
Const NUM_COLORS As Integer = 6
Dim i As Long
Dim t As Double
Dim expr As Double
Dim x As Double
Dim y As Double
Dim colors(0 To NUM_COLORS - 1) As OLE_COLOR

' Initialize colors.
i = 0
colors(i) = RGB(255, 0, 0): i = i + 1
colors(i) = RGB(0, 255, 0): i = i + 1
colors(i) = RGB(0, 0, 255): i = i + 1
colors(i) = RGB(255, 255, 0): i = i + 1
colors(i) = RGB(0, 255, 255): i = i + 1
colors(i) = RGB(255, 0, 255): i = i + 1

' Get the first point.
t = 0
expr = Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5
x = Cos(t) * expr
y = Sin(t) * expr
' I switch these for vertical symmetry.
Me.CurrentX = y
Me.CurrentY = -x
Me.DrawWidth = 2

For i = 1 To NUM_LINES - 1
t = i * 24# * PI / NUM_LINES
expr = Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ _
5
x = Cos(t) * expr
y = Sin(t) * expr

'Line -(y, -x),vbblue
Line -(y, -x), colors((i * 24 / NUM_LINES) Mod _
NUM_COLORS)
Next i
End Sub```

For more information on graphics programming in Visual Basic 6, see my book Ready-to-Run Visual Basic Graphics Programming.