SIGN UP MEMBER LOGIN:    
ARTICLE

Drawing B-Spline Curves

Posted by Avinash Pundit Articles | GDI+ & Graphics April 22, 2002
The attached source code project draws spline curves between two points.
Reader Level:
Download Files:
 

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 struct point
{
public int x;
public int y;
public void setxy(int i,int j)
{
x=i;
y=j;
}
public void clearxy()
{
x=0;
y=0;
}
}
public void BSPLINE(point p1,point p2,point p3,point p4,int divisions)
{
double []a=new double[4];
double []b=new double[4];
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];
for (int i = 1 ; i < divisions ; i++)
{
float t;
t = ((float) i) / ((float) 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]));
}

public void plus_draw(int x,int y,int pen_width,Color cl )
{
Graphics g = Graphics.FromHwnd(this.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);
}
public void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (Movement_Click>3)
{
pt[0]=pt[1];
pt[1]=pt[2];
pt[2]=pt[3];
pt[3].setxy(e.X,e.Y);
int no_of_interpolated_points=(int)(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);
for (int i=0;i<no_of_interpolated_points;i++)
plus_draw((int )spline_out_x[i],(int)spline_out_y[i],2,Color.Blue);
}
else
{
pt[Movement_Click].setxy(e.X,e.Y);
}
Movement_Click=Movement_Click+1;
plus_draw(e.X,e.Y,1,Color.Red);
}

Login to add your contents and source code to this article
share this article :
post comment
 

Hoe can Do it in a Canvas in WPF?

Posted by Prajeesh Prabhakar Mar 05, 2009

good work,but do u know how to draw the nurbs spline ? contact me whit the email: wwei466@sina.com thanks

Posted by king vevi Dec 01, 2008
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor