Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDraw a colored butterfly curve in VB .NET
DescriptionThis example shows how to draw a colored butterfly curve in VB .NET.
Keywordsgraphics, curve, butterfly, butterfly curve, VB.NET
CategoriesGraphics, Algorithms, VB.NET
 

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

The form's Paint event handler loops variable t through the values 0 to 24 * Pi to generate the curve's points and connects them. For each line, the code calls function GetColor to get the line's color.

 
Private Const PERIOD As Integer = 24

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    e.Graphics.Clear(Me.BackColor)

    ' Scale and translate.
    Const YMIN As Double = -4.4
    Const YMAX As Double = 2.7
    Const HGT As Double = YMAX - YMIN
    Dim wid As Double = HGT * Me.ClientSize.Width / _
        Me.ClientSize.Height
    Dim scale As Double = Me.ClientSize.Height / HGT
    e.Graphics.ScaleTransform(scale, scale)
    e.Graphics.TranslateTransform(wid / 2, -YMIN)

    ' Draw the curve.
    Const PI As Double = 3.14159265
    Const NUM_LINES As Long = 5000
    Dim i As Long
    Dim t, expr, x, y As Double
    Dim pt0, pt1 As PointF

    ' Generate the points.
    t = 0
    expr = Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5
    pt1 = New PointF(Sin(t) * expr, -Cos(t) * expr)

    Dim the_pen As New Pen(Color.Blue, 0)
    For i = 1 To NUM_LINES
        t = i * PERIOD * PI / NUM_LINES
        expr = Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ _
            5
        pt0 = pt1
        pt1 = New PointF(Sin(t) * expr, -Cos(t) * expr)
        the_pen.Color = GetColor(t)
        e.Graphics.DrawLine(the_pen, pt0, pt1)
    Next i
    the_pen.Dispose()
End Sub
 
When the program starts, the form's Load event handler sets the form's style to use double buffering and initializes the m_Colors array to a selection of predefined colors.
 
Private m_Colors(PERIOD - 1) As Color

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Me.SetStyle( _
        ControlStyles.AllPaintingInWmPaint Or _
        ControlStyles.DoubleBuffer Or _
        ControlStyles.ResizeRedraw, _
        True)
    Me.UpdateStyles()

    m_Colors = New Color() { _
        Color.Pink, _
        Color.Red, _
        Color.Orange, _
        Color.Yellow, _
        Color.Lime, _
        Color.Cyan, _
        Color.Blue, _
        Color.Violet, _
        Color.Pink, _
        Color.Red, _
        Color.Orange, _
        Color.Yellow, _
        Color.Lime, _
        Color.Cyan, _
        Color.Blue, _
        Color.Violet, _
        Color.Pink, _
        Color.Red, _
        Color.Orange, _
        Color.Yellow, _
        Color.Lime, _
        Color.Cyan, _
        Color.Blue, _
        Color.Violet _
    }
End Sub
 
Function GetColor returns a different color for values of t that are between multiples of PI.
 
Private Function GetColor(ByVal t As Double) As Color
    Return m_Colors(Int(t / PI))
End Function
 
For more information on graphics programming in Visual Basic (albeit version 6), see my book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated