Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#
Article

Bspline in C#

Rate me:
Please Sign up or sign in to vote.
3.23/5 (15 votes)
12 Dec 2006CPOL1 min read 103.5K   5.7K   29   7
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.

Sample screenshot

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.

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

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

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRegression Pin
Unlead15-Apr-10 22:54
Unlead15-Apr-10 22:54 
QuestionStart drawing curve from second point. Pin
GauranG Shah4-Sep-09 2:15
GauranG Shah4-Sep-09 2:15 
QuestionHm, and where is demo project now? Pin
agorby12-Dec-06 9:24
agorby12-Dec-06 9:24 
AnswerRe: Hm, and where is demo project now? Pin
Shailendra Sason12-Dec-06 9:44
Shailendra Sason12-Dec-06 9:44 
GeneralPoor design Pin
hain12-Dec-06 3:46
hain12-Dec-06 3:46 
Generalwhy Pin
noemailz7-Dec-06 10:40
noemailz7-Dec-06 10:40 
GeneralRe: why Pin
shagins8-Dec-06 2:45
shagins8-Dec-06 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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