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 butterfly curve in VB .NET
DescriptionThis example shows how to draw a butterfly curve in VB .NET.
Keywordsgraphics, curve, butterfly, butterfly curve, VB.NET
CategoriesGraphics, 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_Paint event handler loops variable t through the values 0 to 24 * Pi to generate the curve's points and stores their X and Y coordinates in an array of PointF. It then calls the Graphics object's DrawPolygon method to draw the curve all at once. This is quick, although it means the curve is drawn with a single Pen and thus in a single color. For an exercise, try drawing the curve in multiple colors.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' 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 As Double
    Dim expr As Double
    Dim x As Double
    Dim y As Double
    Dim pts(NUM_LINES - 1) As PointF

    ' Generate the points.
    For i = 0 To NUM_LINES - 1
        t = i * 24.0# * PI / NUM_LINES
        expr = Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ _
            5
        pts(i).X = Sin(t) * expr
        pts(i).Y = -Cos(t) * expr
    Next i

    ' Draw the curve.
    Dim the_pen As New Pen(Color.Blue, 0)
    e.Graphics.DrawPolygon(the_pen, pts)
    the_pen.Dispose()
End Sub
 
For more information on graphics programming in Visual Basic (albeit version 6), see my book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated