Bspline in C#






3.23/5 (15 votes)
Drawing Bsplines in C# with the help of GDI+
Introduction
In the mathematical subfield of numerical analysis, a B-spline is a spline function that has minimal support with respect to a given degree, smoothness, and domain partition.
The term B-spline was coined by Isaac Jacob Schoenberg and is short for basis spline.
B-splines can be evaluated in a numerically stable way by the de Boor algorithm. This Bspline code was implemented as part of my third semester project.
Algorithm
The algorithm used for generating is for cubic Bspline.
Cubic B-spline with uniform knot-vector is the most commonly used form of B-spline. The blending function can easily be precalculated, and is equal for each segment in this case. Put in matrix-form, it is as follows:

for
![t \in [0,1]](http://upload.wikimedia.org/math/d/9/a/d9a06fde4663cdd5b1ba693e9127232f.png)
The Code
Two event handlers pictureBox1_MouseUp
and pictureBox1_MouseDown
are used.
MouseUp Event
MouseUp
event is used to print the point where the mouse is clicked and the mousedown
event is used to get the interpolated points and then accordingly draw the lines.
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
Color cl=Color.Black;
g.DrawLine (new Pen(cl, 2), e.X, e.Y, e.X+1 , e.Y+1 );
}
MouseDown Event
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (no_of_points > 3)
{
pt[0] = pt[1];pt[1] = pt[2];pt[2] = pt[3];
pt[3].setxy(e.X, e.Y);
double temp = Math.Sqrt(Math.Pow(pt[2].x - pt[1].x, 2F) +
Math.Pow(pt[2].y - pt[1].y, 2F));
int interpol = System.Convert.ToInt32(temp);
bsp(pt[0],pt[1],pt[2],pt[3],interpol);
int i;
int width=2;
Graphics g = pictureBox1.CreateGraphics();
Color cl=Color.Blue;
int x,y;
// the lines are drawn
for (i = 0; i <= interpol - 1; i++)
{
x=System.Convert.ToInt32(splinex[i]);
y=System.Convert.ToInt32(spliney[i]);
g.DrawLine( new Pen(cl, width) , x - 1, y, x + 1, y);
g.DrawLine(new Pen(cl, width), x, y - 1, x, y + 1);
}
}
else
{
pt[no_of_points].setxy(e.X, e.Y);
}
no_of_points = no_of_points + 1;
}
It first checks whether the number of points are greater than four or not. If the points are greater than 4, the bsp
function is called and then finally the lines are printed.
BSP Function
public void bsp(point p1, point p2, point p3, point p4, int divisions)
{
double[] a = new double[5];
double[] b = new double[5];
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;
splinex[0] = a[3];
spliney[0] = b[3];
int i;
for (i = 1; i <= divisions - 1; i++)
{
float t = System.Convert.ToSingle(i) / System.Convert.ToSingle(divisions);
splinex[i] = (a[2] + t * (a[1] + t * a[0]))*t+a[3] ;
spliney[i] = (b[2] + t * (b[1] + t * b[0]))*t+b[3] ;
}
}
The bsp
function calculates the values by solving the matrix given above.
Conclusion
Thus we can draw Bspline curves by defining our control points by clicking on the picturebox control. The Bsplines have a wide application in the graphics field.