' 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
|