# 采用Cardinal法构造插枝分段三次样条曲线 : 实战篇

 版权声明：   本文由timewolf完成，首发于CSDN，作者保留版权。未经许可，不得使用于任何商业用途。欢迎转载，但请保持文章及版权声明完整。如需联络请发邮件：karla9(AT)eyou(dot)com

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDIplusApplication1
{
#region the defination of MyArrayList
class MyArrayList
{
private ArrayList data = new ArrayList();

public MyArrayList()
{
}

public Object this[int idx]
{
get
{
if (idx > -1 && idx < data.Count)
{
return (data[idx]);
}
else
{
throw new InvalidOperationException("[MyArr.set_Item] Index out of range");
}
}
set
{
if (idx > -1 && idx < data.Count)
{
data[idx] = value;
}
else if (idx == data.Count)
{
}
else if (idx > data.Count)
{
for (int i = data.Count; i < idx; i++)
{
}
}
else
{
throw new InvalidOperationException("[MyArr.set_Item] Index out of range");
}
}
}

public int Count
{
get
{
return data.Count;
}
}

{
}

public Array ToArray(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
Array array1 = Array.CreateInstance(type, data.Count);
array1 = data.ToArray(type);
return array1;
}
}
#endregion

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public struct Point3D
{
public float x;
public float y;
public float z;
}

private MyArrayList mousePoints = new MyArrayList();
Pen redPen = new Pen(Color.Red, 2);
Pen bluePen = new Pen(Color.Blue, 2);
Pen greenPen = new Pen(Color.Green, 2);
Pen brownPen = new Pen(Color.Brown, 2);
PointF lastPoint = new PointF(0, 0);
PointF currentPoint = new PointF(0, 0);
System.Drawing.Graphics g;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
g = this.CreateGraphics();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(640, 526);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Click += new System.EventHandler(this.Form1_Click);

}
#endregion

/// <summary>
/// The main entry PointF for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}

#region Cardinal algorithm
private PointF Cardinal(float u, float t, PointF A, PointF B, PointF C, PointF D)
{
float s = (1 - t) / 2;
PointF resultPoint = new PointF();
resultPoint.X = GetResult(A.X, B.X, C.X, D.X, s, u);
resultPoint.Y = GetResult(A.Y, B.Y, C.Y, D.Y, s, u);
return resultPoint;
}

private float GetResult(float a, float b, float c, float d, float s, float u)
{
float result = 0.0F;
result = a*(2*s*u*u - s*u*u*u - s*u) + b*((2-s)*u*u*u + (s-3)*u*u + 1) +
c*((s-2)*u*u*u + (3-2*s)*u*u + s*u) + d*(s*u*u*u - s*u*u);
return result;
}
#endregion

private void Form1_Click(object sender, System.EventArgs e)
{
Point currentPosition = Control.MousePosition;
PointF currentMousePosition = (PointF)this.PointToClient(currentPosition);
currentPoint = currentMousePosition;

if (mousePoints.Count == 8)
{
// the system curve, it's good
GraphicsPath path = new GraphicsPath();
PointF[] newPoints = path.PathPoints;
System.Drawing.Drawing2D.Matrix matrix = new Matrix();
g.DrawLines(bluePen, (PointF[])mousePoints.ToArray(typeof(PointF)));
g.DrawPath(greenPen, path);

ArrayList newMousePoints = new ArrayList();
PointF startPoint = (PointF)mousePoints[0];
PointF starterPoint = new PointF(startPoint.X, startPoint.Y);
PointF endPoint = (PointF)mousePoints[mousePoints.Count - 1];
PointF enderPoint = new PointF(endPoint.X, endPoint.Y);

for (int i = 0; i < mousePoints.Count; i++)
{
}

DateTime nowTime = System.DateTime.Now;
TimeSpan startSpan = new TimeSpan(nowTime.Day, nowTime.Hour, nowTime.Minute, nowTime.Second, nowTime.Millisecond);

for (int i = 0; i < 7; i++)
{
float uVaule = 0.000F;
float step = 0.002F;
ArrayList ctlPoints = new ArrayList();
while (uVaule < 1.0000F)
{
PointF newPoint = Cardinal(uVaule, 0.0F, (PointF)newMousePoints[i], (PointF)newMousePoints[i+1],
(PointF)newMousePoints[i+2], (PointF)newMousePoints[i+3]);
uVaule += step;
}
g.DrawLines(redPen, (PointF[])ctlPoints.ToArray(typeof(PointF)));
}

DateTime endTime = System.DateTime.Now;
TimeSpan endSpan = new TimeSpan(endTime.Day, endTime.Hour, endTime.Minute, endTime.Second, endTime.Millisecond);
TimeSpan elipSpan = endSpan.Subtract(startSpan);
Console.WriteLine(elipSpan.ToString());
}
}

}
}

#### GDI+ 中的基数样条,基数样条曲线的Demo

2010-11-22 10:18:00

#### 穿过已知点画平滑曲线（3次贝塞尔曲线）

2012-03-28 09:38:05

#### Cubic spline（三次样条插值）（转载）

2017-08-17 16:46:33

#### 绘制贝赛尔样条,C#源代码Point[] p = {

2008年12月31日 33KB 下载

#### 三次样条插值

2017-01-09 20:22:30

#### 问题六十一：三次b样条(b-spline)曲线的控制点和曲线形状的对应——以回旋体的“基本曲线”为例（2）

2017-01-20 12:45:44

#### 3次B样条曲线

2013-06-24 13:35:32

#### 四种B样条的C#绘制程序

2016年04月20日 108KB 下载

#### 用C#绘制样条曲线又一例, Point[] points = {new Point(60, 60),

2008年12月31日 33KB 下载

#### C#实现赛贝尔等三种曲线的生成算法

2014年01月19日 51KB 下载