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 gr As Graphics, ByVal the_pen As _
Pen, ByVal dt As Single, ByVal tension As Single, ByVal _
pts() As PointF)
Dim control_scale As Single = CSng(tension / 0.5 * _
0.175)
Dim pt, pt_before, pt_after, pt_after2, Di, DiPlus1 As _
PointF
Dim p1, p2, p3, p4 As PointF
For i As Integer = 0 To pts.GetUpperBound(0) - 1
pt_before = pts(Math.Max(i - 1, 0))
pt = pts(i)
pt_after = pts(i + 1)
pt_after2 = pts(Math.Min(i + 2, _
pts.GetUpperBound(0)))
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(gr, the_pen, dt, p1, p2, p3, p4)
Next i
End Sub
|