|
|
Title | Draw a colored chrysanthemum curve in VB .NET |
Description | This example shows how to draw a colored chrysanthemum curve in VB .NET. |
Keywords | graphics, curve, chrysanthemum, chrysanthemum curve, VB.NET |
Categories | Graphics, Algorithms, 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's Paint event handler loops variable t through the values 0 to 21 * 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 = 21
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, r, x, y As Double
Dim pt0, pt1 As PointF
' Generate the points.
t = 0
r = 5 * (1 + Sin(11 * t / 5)) - 4 * Sin(17 * t / 3) ^ 4 _
* Sin(2 * Cos(3 * t) - 28 * t) ^ 8
pt1 = New PointF(r * Sin(t), -r * Cos(t))
Dim the_pen As New Pen(Color.Blue, 0)
For i = 0 To NUM_LINES
t = i * PERIOD * PI / NUM_LINES
r = 5 * (1 + Sin(11 * t / 5)) - 4 * Sin(17 * t / 3) _
^ 4 * Sin(2 * Cos(3 * t) - 28 * t) ^ 8
pt0 = pt1
pt1 = New PointF(r * Sin(t), -r * Cos(t))
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
|
|
|
|
|
|