ARTICLE

# Drawing B-Spline Curves using GDI+ in VB.NET

Posted by | November 08, 2012
Tags:
The attached source code project draws spline curves between two points. Its a cubic spline fitting means program start drawing curve after four clicks.

The attached source code project draws spline curves between two points. Its a cubic spline fitting means program start drawing curve after four clicks.

To run the project, download and unzip the attached file, build and run the project and click on the form.

Main source code:

Public Structure point
Public x As Integer
Public
y As Integer
Public
Sub setxy(ByVal i As Integer, ByVal j As Integer)
x = i
y = j
End Sub 'setxy
Public Sub clearxy()
x = 0
y = 0
End Sub 'clearxy
End Structure 'point
Sub BSPLINE(ByVal p1 As Point, ByVal p2 As Point, ByVal p3 As Point, ByVal p4 As Point, ByVal divisions As Integer)
Dim a(4) As Double
Dim
b(4) As
Double
a(0) = (-p1.x + 3 * p2.x - 3 * p3.x + p4.x) / 6.0
a(1) = (3 * p1.x - 6 * p2.x + 3 * p3.x) / 6.0
a(2) = (-3 * p1.x + 3 * p3.x) / 6.0
a(3) = (p1.x + 4 * p2.x + p3.x) / 6.0
b(0) = (-p1.y + 3 * p2.y - 3 * p3.y + p4.y) / 6.0
b(1) = (3 * p1.y - 6 * p2.y + 3 * p3.y) / 6.0
b(2) = (-3 * p1.y + 3 * p3.y) / 6.0
b(3) = (p1.y + 4 * p2.y + p3.y) / 6.0
spline_out_x(0) = a(3)
spline_out_y(0) = b(3)
Dim i As Integer
For
i = 1 To divisions - 1
Dim t As Single
t = CSng(i) / CSng(divisions)
spline_out_x(i) = a(3) + t * (a(2) + t * (a(1) + t * a(0)))
spline_out_y(i) = b(3) + t * (b(2) + t * (b(1) + t * b(0)))
Next i
End Sub 'BSPLINE
Public Sub plus_draw(ByVal x As Integer, ByVal y As Integer, ByVal pen_width As Integer, ByVal cl As Color)
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
g.DrawLine(
New Pen(cl, pen_width), x - 3, y, x + 3, y)
g.DrawLine(New Pen(cl, pen_width), x, y - 3, x, y + 3)
End Sub 'plus_draw
Public Sub Form_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If Movement_Click > 3 Then
pt(0) = pt(1)
pt(1) = pt(2)
pt(2) = pt(3)
pt(3).setxy(e.X, e.Y)
Dim no_of_interpolated_points As Integer = CInt(Math.Sqrt((Math.Pow(pt(2).x - pt(1).x, 2) + Math.Pow(pt(2).y - pt(1).y, 2))))
BSPLINE(pt(0), pt(1), pt(2), pt(3), no_of_interpolated_points)
Dim i As Integer
For
i = 0 To no_of_interpolated_points - 1
plus_draw(CInt(spline_out_x(i)), CInt(spline_out_y(i)), 2, Color.Blue)
Next i
Else
pt(Movement_Click).setxy(e.X, e.Y)
End If
Movement_Click = Movement_Click + 1
plus_draw(e.X, e.Y, 1, Color.Red)
End Sub 'Form_MouseUp

 This Feature is Sponsored By .NET Charting component with full set of charting and gauge types