Home

GDI+ Shapes: A Closed Curve

 

Description

If you use either the DrawLines(), the DrawBezier() or the DrawBeziers() methods, you would get a continuous line or a series of lines that has a beginning and an end. Alternatively, GDI+ allows you to draw a series of lines but join the end of the last line to the beginning of the first line to have a closed shape. To draw this figure, you can call the Graphics.DrawClosedCurve() method that is overloaded in four versions. Two of them have the following syntaxes:

Public Sub DrawClosedCurve(pen As Pen, points As Point())
Public Sub DrawClosedCurve(pen As Pen, points As PointF())

These two versions are the easiest to use. They allow you to provide an array of four Point or four PointF values. When executed, each of these methods draws a curve that passes through each coordinate and closes the curve by joining the end point to the first unless both points are the same. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim penCurrent As Pen = New Pen(Color.Blue)
            Dim pt As Point() = {New Point(40, 42), New Point(188, 246), _
                              New Point(484, 192), New Point(350, 48)}

            e.Graphics.DrawClosedCurve(penCurrent, pt)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Closed Curve

The first two versions are used to draw the lines but curve them in order to make the shape appear smooth. If you want, you can draw the lines straight from one point to the next without curving them. Using this scenario, the above shape would appear as follows:

Closed Shape With Straight Lines

To draw this type of shape using the DrawClosedCurve() method, you can use one of the following versions of the method:

Public Sub DrawClosedCurve(pen As Pen, points As Point(), _
		              tension As Single, fillmode As FillMode)
Public Sub DrawClosedCurve(pen As Pen, points As PointF(), _
		              tension As Single, fillmode As FillMode)

These versions allow you to specify the tension and the fill mode. The tension factor allow you to specify how much curve would be applied. If this value is passed as 0.00, the points would be joined with straight lines. Otherwise, you can apply a tension using an appropriate decimal value.

The fillmode factor determines how the interior of the curve would be filled. It is controlled through the FillMode enumerator that is defined in the System.Drawing.Drawing2D namespace. The FillMode enumerator has two members: Alternate and Winding. Here is an example:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormPaint(ByVal sender As Object, _
                              ByVal e As PaintEventArgs) _
                              Handles MyBase.Paint

            Dim penCurrent As Pen = New Pen(Color.Red)
            Dim pt As Point() = {New Point(40, 42), New Point(188, 246), _
                              New Point(484, 192), New Point(350, 48)}

            e.Graphics.DrawClosedCurve(penCurrent, pt, 0.75, _
                     FillMode.Winding)

        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

This would produce:

Closed Curve

Remember that the higher the tension, the sharper the corners. If you want the shape to show straight lines, pass a tension of 0.00F.

 

Home Copyright © 2008-2016, FunctionX, Inc. Home