採用Cardinal法構造插枝分段三次樣條曲線 : 實戰篇
本文由timewolf完成,首發於CSDN,作者保留版權。 |
下面會給出一個簡單的例子: 在視窗上用滑鼠點8個點,然後就會將這8個點的座標畫出來~~~, 我共總用了3支畫筆: 一支綠筆是將這些點用GDI+的畫法畫出來,一支藍筆是將這些點畫直線畫出來,第三支紅筆使用我的Cardinal方法畫出來,從結果中我們可以看到,當t==0的時候,Cardinal畫出來的曲線和GDI+的曲線完全重合
開發環境:VS.net 2003
使用方法:新建一個windows form專案, 將下面的程式碼拷貝至Form1.cs中覆蓋之,然後run就行,然後就隨便用滑鼠點8個點吧
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)
{
data.Add(value);
}
else if (idx > data.Count)
{
for (int i = data.Count; i < idx; i++)
{
data.Add(null);
}
data.Add(value);
}
else
{
throw new InvalidOperationException("[MyArr.set_Item] Index out of range");
}
}
}
public int Count
{
get
{
return data.Count;
}
}
public void Add(Object obj)
{
data.Add(obj);
}
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>
[STAThread]
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);
mousePoints.Add(currentMousePosition);
currentPoint = currentMousePosition;
if (mousePoints.Count == 8)
{
// the system curve, it's good
GraphicsPath path = new GraphicsPath();
path.AddCurve((PointF[])mousePoints.ToArray(typeof(PointF)));
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);
newMousePoints.Add(starterPoint);
for (int i = 0; i < mousePoints.Count; i++)
{
newMousePoints.Add(mousePoints[i]);
}
newMousePoints.Add(enderPoint);
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();
ctlPoints.Add(newMousePoints[i + 1]);
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]);
ctlPoints.Add(newPoint);
uVaule += step;
}
ctlPoints.Add(newMousePoints[i + 2]);
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());
}
}
}
}