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 Bezier curve by hand in VB .NET
DescriptionThis example shows how to draw a Bezier curve by hand in VB .NET.
KeywordsBezier curve, spline, smooth, curve
CategoriesGraphics, VB.NET
 
A Bezier curve is defined by four control points p0, p1, p2, and p3. It starts at p0 heading toward p1, and ends at p3 coming from the direction of p2. You can define the Bezier curve by the following equation:

    p0 * (1 - t) ^ 3 +
    p1 * 3 * t * (1 - t) ^ 2 +
    p2 * 3 * t ^ 2 * (1 - t) +
    p3 * t ^ 3

Here t is a parametric variable that rannges from 0 to 1.

The following X and Y functions return values of this equation for values of t and the control points' coordinates. Subroutine DrawBezier loops variable t from 0 to 1, uses the functions to generate the points on the Bezier curve, and connects them.

 
' Parametric functions for drawing a degree 3 Bezier curve.
Private Function X(ByVal t As Single, ByVal x0 As Single, _
    ByVal x1 As Single, ByVal x2 As Single, ByVal x3 As _
    Single) As Single
    X = CSng( _
        x0 * (1 - t) ^ 3 + _
        x1 * 3 * t * (1 - t) ^ 2 + _
        x2 * 3 * t ^ 2 * (1 - t) + _
        x3 * t ^ 3 _
        )
End Function

Private Function Y(ByVal t As Single, ByVal y0 As Single, _
    ByVal y1 As Single, ByVal y2 As Single, ByVal y3 As _
    Single) As Single
    Y = CSng( _
        y0 * (1 - t) ^ 3 + _
        y1 * 3 * t * (1 - t) ^ 2 + _
        y2 * 3 * t ^ 2 * (1 - t) + _
        y3 * t ^ 3 _
        )
End Function

' Draw the Bezier curve.
Public Sub DrawBezier(ByVal gr As Graphics, ByVal the_pen _
    As Pen, ByVal dt As Single, ByVal pt0 As PointF, ByVal _
    pt1 As PointF, ByVal pt2 As PointF, ByVal pt3 As PointF)
    ' Debugging code commented out.
    ' Draw the control lines.
    Dim dashed_pen As New Pen(Color.Black)
    dashed_pen.DashStyle = Drawing2D.DashStyle.Custom
    dashed_pen.DashPattern = New Single() {4, 4}
    gr.DrawLine(dashed_pen, pt0.X, pt0.Y, pt1.X, pt1.Y)
    gr.DrawLine(dashed_pen, pt2.X, pt2.Y, pt3.X, pt3.Y)
    dashed_pen.Dispose()

    ' Draw the curve.
    Dim t, x0, y0, x1, y1 As Single

    t = 0.0
    x1 = X(t, pt0.X, pt1.X, pt2.X, pt3.X)
    y1 = Y(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
    t += dt
    Do While t < 1.0
        x0 = x1
        y0 = y1
        x1 = X(t, pt0.X, pt1.X, pt2.X, pt3.X)
        y1 = Y(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
        gr.DrawLine(the_pen, x0, y0, x1, y1)
        t += dt
    Loop

    ' Connect to the final point.
    t = 1.0
    x0 = x1
    y0 = y1
    x1 = X(t, pt0.X, pt1.X, pt2.X, pt3.X)
    y1 = Y(t, pt0.Y, pt1.Y, pt2.Y, pt3.Y)
    gr.DrawLine(the_pen, x0, y0, x1, y1)
End Sub
 
For more information on graphics programming in Visual Basic 6, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated