Copyright:
This article is completed by timewolf and first published on csdn. The author reserves the copyright. It shall not be used for any commercial purposes without permission. You are welcome to reprint the article, but please keep the article and copyright statement complete. If you need to contact us, please send an email: karla9 (AT) eyou (DOT) com
|
The following is a simple example: Click eight points in the window and draw the coordinates of these eight points ~~~, I used three paint brushes in total: a green pen draws these points using the GDI + method, and a blue pen draws these dots in a straight line, the third red pen is drawn using my cardinal method. From the result, we can see that when T = 0, the curve drawn by Cardinal completely overlaps with the curve of GDI +.
Development Environment: vs.net 2003
Usage: Create a Windows form project, copy the following code to form1.cs and overwrite it, run it, and click 8 at will.
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)
{
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-s * u) + B * (2-S) * u + (S-3) * u + 1) +
C * (S-2) * u + (3-2 * s) * u + S * U) + D * (S * 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.20.f)
{
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 ());
}
}
}
}