12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "PolySpline.h"
- USING_NS_BF;
-
- PolySpline2D::PolySpline2D()
- {
- mCoefs = NULL;
- }
- PolySpline2D::~PolySpline2D()
- {
- delete mCoefs;
- }
- void PolySpline2D::AddPt(float x, float y)
- {
- delete mCoefs;
- mCoefs = NULL;
-
-
- mInputPoints.push_back(PointF(x, y));
- }
- int PolySpline2D::GetLength()
- {
- return (int) mInputPoints.size();
- }
- void PolySpline2D::Calculate()
- {
- int n = (int) mInputPoints.size();
- float* mat = new float[n*n];
- mCoefs = new float[n];
- for (int j=0; j<n; j++)
- mat[j*n] = mInputPoints[j].y;
- for (int i=1; i<n; i++)
- {
- for (int j=0; j<n-i; j++)
- mat[i+j*n]=(mat[(i-1)+j*n]-mat[(i-1)+(j+1)*n])/(mInputPoints[j].x-mInputPoints[j+i].x);
- }
- for (int i=0; i<n; i++)
- mCoefs[i] = mat[i];
- delete [] mat;
- }
- float PolySpline2D::Evaluate(float x)
- {
- if (mCoefs == NULL)
- Calculate();
- float result = mCoefs[0];
- int n = (int) mInputPoints.size();
- for (int i = 1; i < n; i++)
- {
- float add = mCoefs[i];
- for (int j = 0; j < i; j++)
- add *= (x - mInputPoints[j].x);
- result += add;
- }
- return result;
- }
|