This is an example of me a mathematics teacher (professor, mathematics master, often doing an algorithm), used for multiple discrete points to fit the smooth curve, he optimizes catching the law, this example is suitable for closure and not close Happening. At that time, due to the engineering situation, the code was not good, it was very easy to use. In order to facilitate communication parameters, I have made a list, and it can be modified according to my situation, and the core algorithm can not move.
Class cfoldpoint
{public:
Double X; Double Y;
}
TypedEf CTypedPtrlist
CfoldPointList;
Typedef Carray
CdoubleArray;
Three functions, SPLINE calls the other two. Time to call the spline function directly, the entry PLIST is known for the discrete point linked list, and PDestList is a list of generated points. SM is the number of intermediate insertions in two points, and continue = 0 is no regular sampling point, requiring a closed curve. 1 is the sampling point X coordinate continuous 2 is Y continuous
Void ZG (CDOUBLARRAY * A, CDOUBLARRAY * B, CDOUBLARRAY * C, CDOUBLARRAY * G, INT & logi) {// catching up register long i; int N; n = a-> getSize (); if (logi == 0) {(* C) [0] = (* c) [0] / (* b) [0]; for (i = 1; i
{
(* B) [i] = (* b) [i] - (* a) [i] * (* c) [i-1];
(* C) [i] = (* c) [i] / (* b) [i];
}
(* A) [0] = 0 .;
(* C) [N-1] = 0 .;
Logi = 1;
}
(* G) [0] = (* g) [0] / (* b) [0];
For (i = 1; i
{
(* G) [i] = ((* g) [i] - (* a) [i] * (* g) [i-1]) / (* b) [i];
}
For (i = N-2; I> -1; i -) // DO 30 i = N-1, 1, -1
{
(* G) [i] = (* g) [i] - (* c) [i] * (* g) [i 1];
}
Return;
}
void SPLine4 (CDoubleArray * X, CDoubleArray * Y, double & XI, double & YI, CDoubleArray * A, CDoubleArray * B, CDoubleArray * C, CDoubleArray * G, int & LOGI, int MD) {register long I; double W1, W2, H; INT n = x-> getSize (); if (logi == 0) {for (i = 1; i
{
(* B) [i] = (* x) [i] - (* x) [i-1];
(* C) [i] = (* y) [i] - (* y) [i-1]) / (* b) [i];
}
For (i = 1; i
{
(* A) [i] = (* b) [i] (* b) [i 1];
(* G) [i] = 6. * ((* C) [i 1] - (* c) [i]) / (* a) [i];
(* A) [i] = (* b) [i] / (* a) [i];
}
For (i = 1; i
{
(* C) [i] = 1 .- (* a) [i];
(* B) [i] = 2 .;
}
(* B) [0] = 2 .;
(* B) [N-1] = 2 .;
IF (MD == 3)
{
(* C) [0] = - 1 .;
(* A) [N-1] = - 1 .;
(* A) [0] = 0 .;
(* C) [N-1] = 0 .;}
ZG (A, B, C, G, LOGI);
}
For (i = 1; i
{
IF (xi> = (* x) [i-1] && xi <= (* x) [i]) // gele
{
H = (* x) [i] - (* x) [i-1];
W1 = (* x) [i] -xi;
W2 = xi - (* x) [i-1];
Yi = W1 * W1 * W1 * (* g) [i-1] /6./H;
Yi = yi w2 * w2 * w2 * (* g) [i] /6./H;
Yi = yi w1 * ((* y) [i-1] - (* g) [i-1] * h * h / 6.) / H;
Yi = yi w2 * ((* y) [i] - (* g) [i] * h * h / 6.) / H;
}
}
}
Void spline (cfoldpointlist * plist, cfoldpointlist * pdestlist, int sm, int company = 0)
{
CfoldPoint * pfoldhead, * pfoldtail;
Position POS;
CDoubleArray A, B, C, G, X, Y, T;
Double Xi, Yi, XX, YY;
Register long i;
Long N;
Int logi;
Long realsm;
Long BEI, YU;
CfoldPoint * pfold;
FILE: // Getting the initial value
N = pList-> getCount ();
A.setsize (n);
B.setsize (n);
C.setsize (n);
G.setsize (n);
X.setsize (n);
Y.setsize (n);
T.setsize (n);
Realsm = (n-1) * SM N;
POS = PLIST-> GetHeadPosition ();
For (i = 0; i
{
PFOLD = PLIST-> GetNext (POS);
X [i] = pfold-> x;
Y [i] = pfold-> y;
}
Pfoldhead = plist-> gethead ();
PFOLDTAIL = PLIST-> gettail ();
IF (Continue == 0) // pfoldhead-> x == pfoldtail-> x&& pfoldhead-> y == pfoldtail-> y)
{
File: // Closed
T [0] = 0;
For (i = 0; i
{
T [i 1] = t [i] calculatedIndance (x [i], y [i], x [i 1], y [i 1]) 0.000000001;
}
Logi = 0;
Yi = 0;
For (i = 0; i
{
BEI = I / (SM 1);
YU = I% (SM 1);
IF (yu! = 0)
{
Xi = T [BEI] (T [BEI 1] -T [BEI]) / (SM 1) * yu;
SPline4 (& T, & Y, Xi, Yi, & A, & B, & C, & g, logi, 3);
YY = yi; // y [bei];
}
Else
{
YY = y [bei];
}
PFOLD = New cfoldpoint;
PFOLD-> Y = YY;
PDestList-> addtail (pfold);
}
Logi = 0;
Yi = 0;
POS = pdestlist-> getHeadPosition ();
For (i = 0; i
{
BEI = I / (SM 1); yu = i% (SM 1);
IF (yu! = 0)
{
Xi = T [BEI] (T [BEI 1] -T [BEI]) / (SM 1) * yu;
Spline4 (& T, & X, Xi, Yi, & A, & B, & C, & G, LOGI, 3);
YY = yi; // x [bei];
}
Else
{
YY = x [BEI];
}
PFOLD = PDestList-> getNext (POS);
PFOLD-> x = yy;
}
}
Else IF (Continue == 1)
{
FILE: // X Continuous
Logi = 0;
Yi = 0;
For (i = 0; i
{
BEI = I / (SM 1);
YU = I% (SM 1);
IF (yu! = 0)
{
Xi = x [bei] (x [bei 1] -X [bei]) / (SM 1) * yU;
SPline4 (& X, & Y, Xi, Yi, & A, & B, & C, & G, LOGI, 3);
XX = xi;
YY = yi;
}
Else
{
Xx = x [bei];
YY = y [bei];
}
PFOLD = New cfoldpoint;
PFOLD-> x = xx;
PFOLD-> Y = YY;
PDestList-> addtail (pfold);
}
}
Else
{
File: // Y continuous
Logi = 0;
Yi = 0;
For (i = 0; i
{
BEI = I / (SM 1);
YU = I% (SM 1);
IF (yu! = 0)
{
Xi = y [bei] (y [bei 1] -y [bei]) / (SM 1) * YU;
Spline4 (& Y, & X, Xi, Yi, & A, & B, & C, & G, Logi, 3);
Xx = yi;
Yy = xi;
}
Else
{
Xx = x [bei];
YY = y [bei];
}
PFOLD = New cfoldpoint;
PFOLD-> x = xx;
PFOLD-> Y = YY;
PDestList-> addtail (pfold);
}
}
Return;
}