13,358,478 members (51,769 online)
alternative version

#### Stats

78.9K views
28 bookmarked
Posted 7 Dec 2006

# Bspline in C#

, 12 Dec 2006
 Rate this:
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:

$\mathbf{S}_i(t) = \begin{bmatrix} t^3 & t^2 & t & 1 \end{bmatrix} \frac{1}{6} \begin{bmatrix} -1 & 3 & -3 & 1 \\ 3 & -6 & 3 & 0 \\ -3 & 0 & 3 & 0 \\ 1 & 4 & 1 & 0 \end{bmatrix} \begin{bmatrix} \mathbf{p}_{i-1} \\ \mathbf{p}_{i} \\ \mathbf{p}_{i+1} \\ \mathbf{p}_{i+2} \end{bmatrix}$

for

$t \in [0,1]$

## 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.

</dd />

## About the Author

 Software Developer India
No Biography provided

## Comments and Discussions

 First Prev Next
 Start drawing curve from second point. GauranG Shah4-Sep-09 3:15 GauranG Shah 4-Sep-09 3:15
 Hm, and where is demo project now? agorby12-Dec-06 10:24 agorby 12-Dec-06 10:24
 Re: Hm, and where is demo project now? Shailendra Sason12-Dec-06 10:44 Shailendra Sason 12-Dec-06 10:44
 Poor design hain12-Dec-06 4:46 hain 12-Dec-06 4:46
 why noemailz7-Dec-06 11:40 noemailz 7-Dec-06 11:40
 Re: why shagins8-Dec-06 3:45 shagins 8-Dec-06 3:45
 Last Visit: 31-Dec-99 19:00     Last Update: 24-Jan-18 2:38 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.