|
|
Title | Draw a smooth curve |
Description | This example shows how to draw a smooth curve in VB 6. |
Keywords | smooth curve, spline, cardinal spline, Bezier curve, tension |
Categories | Graphics |
|
|
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.
A special case occurs at the first and last points, which have only one neighbor. At these points, the control points lie along the line between the end point and its only neighbor.
|
|
' Draw a cardinal spline built from connected Bezier curves.
Public Sub DrawCurve(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
control_scale = CSng(tension / 0.5 * 0.175)
For i = LBound(pts) To UBound(pts) - 1
pt_before = pts(Max(i - 1, 0))
pt = pts(i)
pt_after = pts(i + 1)
pt_after2 = pts(Min(i + 2, UBound(pts)))
p1 = pts(i)
p4 = pts(i + 1)
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 smooth closed curve for information on drawing closed curves.
For more information on graphics programming in Visual Basic 6, see my book Visual Basic Graphics Programming.
|
|
|
|
|
|