Programming a Chrysanthemum curve in .NET

Having a curious mind, I always try to figure out how things work and how things can be done. I saw a movie last month on Netflix, whose name eludes me. In this movie, however, I saw these beautiful flowers. I had to do some research to find the name, which turned out to be Chrysanthemum. The word Chrysanthemum itself is derived from ancient Greek, where chrysos means gold and anthemon means flower.

Then I started wondering what the math involved to create the curves of one of these flowers would be and whether it would be possible to create this type of shape in .NET. With that in mind, I put together this quick tutorial showcasing how to work with shapes and curves in .NET. Let us have a look.

Read: Introducing the .NET Coding Pack

Working with Shapes and Curves in .NET

To start, open Visual Studio and create a Windows Forms project. Be sure to resize your form to be relatively large. Then, set a nice Background color, as we will be working with a lot of assorted colors; I would recommend setting a black background color.

Next, import the Math namespace, because we will be working with various math formulas. Here is the code to import the Math namespace in .NET:

Imports System.Math

Next, create some start-up fields such as an array for the assorted colors and specify the number of lines we would like to draw:

    Private Const intLeaves As Integer = 18
    Private arrColors(intLeaves - 1) As Color

Edit your Form’s Load event to look as follows:

    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()

        arrColors = New Color() {
        Color.DarkMagenta,
        Color.Brown,
        Color.Orange,
        Color.SeaShell,
        Color.Beige,
        Color.Aquamarine,
        Color.DodgerBlue,
        Color.Purple,
        Color.Fuchsia,
        Color.Red,
        Color.Chocolate,
        Color.White,
        Color.DarkGreen,
        Color.Gold,
        Color.Blue,
        Color.Violet,
        Color.Pink,
        Color.MistyRose,
        Color.Orange,
        Color.Yellow,
        Color.Chartreuse,
        Color.Teal,
        Color.SkyBlue,
        Color.MediumOrchid
        }


    End Sub

First, we enable smooth drawing and smooth responsiveness on our drawing. This means that there will be no issues in resizing the form and it will look smooth and not pixelated. Then, we specify the assorted colors that we will be using to draw. Obviously, you can edit in your own colors – feel free to experiment.

Next, edit your Form’s Paint event to resemble the following code segment:

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Const dbMinHeight As Double = -11
        Const dbMaxHeight As Double = 11

        Const dbTotalHeight As Double = dbMaxHeight - dbMinHeight

        Dim dbTotalWidth As Double = dbTotalHeight * Me.ClientSize.Width / Me.ClientSize.Height

        Dim dbMiddle As Double = Me.ClientSize.Height / dbTotalHeight

        e.Graphics.ScaleTransform(dbMiddle, dbMiddle)
        e.Graphics.TranslateTransform(dbTotalWidth / 2, -dbMinHeight)

        Const PI As Double = 3.14159265
        Const lngLines As Long = 7500

        Dim i As Long
        Dim a
        Dim b As Double
        Dim ptOne As PointF
        Dim ptTwo As PointF

        a = 0
        ptTwo = New PointF(b * Sin(a), -b * Cos(a))

        Dim pPen As New Pen(Color.Blue, 0)

        For i = 0 To lngLines
            a = i * intLeaves * PI / lngLines

            b = 5 * (1 + Sin(11 * a / 5)) - 4 * Sin(30 * a / 3) ^ 4 * Sin(2 * Cos(3 * a) - 14 * a) ^ 6
            ptOne = ptTwo

            ptTwo = New PointF(b * Sin(a), -b * Cos(a))

            pPen.Color = GetNewColour(a)

            e.Graphics.DrawLine(pPen, ptOne, ptTwo)

        Next i

        pPen.Dispose()

    End Sub

In the above .NET code example, we determine the drawing area, commence drawing in the centre of the form, and let the drawn lines spread out outwards. We then calculate the positions of the lines. This can also be edited according to your needs. Lastly, we determine the color to draw with the help of the GetNewColour function, as shown below:

    Private Function GetNewColour(c As Double) As Color

        Return arrColors(Int(c / PI))

    End Function

Running the code, my output looks like the following image:

Drawing Curves in .NET

Read more .NET programming tutorials and software developments guides.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read