关闭

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

标签: floatcomponentsgdi+matrixwindowsobject
2204人阅读 评论(2) 收藏 举报

 

版权声明:

 

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

下面会给出一个简单的例子: 在窗口上用鼠标点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());
   }
  }

 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:110281次
    • 积分:1502
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:21篇
    • 译文:0篇
    • 评论:20条
    文章分类
    最新评论