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 chrysanthemum curve in VB .NET
DescriptionThis example shows how to draw a chrysanthemum curve in VB .NET.
Keywordsgraphics, curve, chrysanthemum, chrysanthemum curve, VB.NET
CategoriesGraphics, VB.NET
 
This program uses the following equations to draw the chrysanthemum curve:

    r = 5 * (1 + Sin(11 * t / 5)) -
        4 * Sin(17 * t / 3) ^ 4 * Sin(2 * Cos(3 * t) - 28 * t) ^ 8
    x = r * Cos(t)
    y = r * Sin(t)

The Form_Paint event handler loops variable t through the values 0 to 21 * 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 = -11
    Const YMAX As Double = 11
    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 r 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 * 21.0# * PI / NUM_LINES
        r = 5 * (1 + Sin(11 * t / 5)) - 4 * Sin(17 * t / 3) _
            ^ 4 * Sin(2 * Cos(3 * t) - 28 * t) ^ 8
        pts(i).X = r * Sin(t)
        pts(i).Y = -r * Cos(t)
    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