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 smooth closed curve
DescriptionThis example shows how to draw a smooth closed curve in VB 6.
Keywordssmooth curve, smooth closed curve, spline, cardinal spline, Bezier curve, tension
CategoriesGraphics
 
This approach connects a series of points with Bezier curves. The interior control points are chosen so the curves meet smoothly. These control points lie along a line parallel to the line connecting the points' neighbors. For example, suppose point p2 lies between p1 and p3. When drawing the Bezier curve between points p2 and p3, the control point after p2 lies along a line starting at p2 and going in the same direction as the line between p1 and p3. The distance along this line that the point lies is determined by a "tension" factor. See the code for details.
 
' Draw a closed cardinal spline built from connected Bezier
' curves.
Public Sub DrawClosedCurve(ByVal pic As Object, ByVal dt As _
    Single, ByVal tension As Single, pts() As PointF)
Dim control_scale As Single
Dim pt As PointF
Dim pt_before As PointF
Dim pt_after As PointF
Dim pt_after2 As PointF
Dim Di As PointF
Dim DiPlus1 As PointF
Dim p1 As PointF
Dim p2 As PointF
Dim p3 As PointF
Dim p4 As PointF
Dim i As Integer
Dim num_pts As Integer

    control_scale = CSng(tension / 0.5 * 0.175)
    num_pts = UBound(pts) + 1
    For i = 0 To UBound(pts)
        pt_before = pts((i - 1 + num_pts) Mod num_pts)
        pt = pts(i)
        pt_after = pts((i + 1) Mod num_pts)
        pt_after2 = pts((i + 2) Mod num_pts)

        p1 = pt
        p4 = pt_after

        Di.X = pt_after.X - pt_before.X
        Di.Y = pt_after.Y - pt_before.Y
        p2.X = pt.X + control_scale * Di.X
        p2.Y = pt.Y + control_scale * Di.Y

        DiPlus1.X = pt_after2.X - pts(i).X
        DiPlus1.Y = pt_after2.Y - pts(i).Y
        p3.X = pt_after.X - control_scale * DiPlus1.X
        p3.Y = pt_after.Y - control_scale * DiPlus1.Y

        DrawBezier pic, dt, p1, p2, p3, p4
    Next i
End Sub
 
See Draw a Bezier curve for information on drawing Bezier curves.

Also see Draw a closed curve for information on drawing non-closed curves.

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