PolySpline.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "PolySpline.h"
  2. USING_NS_BF;
  3. PolySpline2D::PolySpline2D()
  4. {
  5. mCoefs = NULL;
  6. }
  7. PolySpline2D::~PolySpline2D()
  8. {
  9. delete mCoefs;
  10. }
  11. void PolySpline2D::AddPt(float x, float y)
  12. {
  13. delete mCoefs;
  14. mCoefs = NULL;
  15. mInputPoints.push_back(PointF(x, y));
  16. }
  17. int PolySpline2D::GetLength()
  18. {
  19. return (int) mInputPoints.size();
  20. }
  21. void PolySpline2D::Calculate()
  22. {
  23. int n = (int) mInputPoints.size();
  24. float* mat = new float[n*n];
  25. mCoefs = new float[n];
  26. for (int j=0; j<n; j++)
  27. mat[j*n] = mInputPoints[j].y;
  28. for (int i=1; i<n; i++)
  29. {
  30. for (int j=0; j<n-i; j++)
  31. mat[i+j*n]=(mat[(i-1)+j*n]-mat[(i-1)+(j+1)*n])/(mInputPoints[j].x-mInputPoints[j+i].x);
  32. }
  33. for (int i=0; i<n; i++)
  34. mCoefs[i] = mat[i];
  35. delete [] mat;
  36. }
  37. float PolySpline2D::Evaluate(float x)
  38. {
  39. if (mCoefs == NULL)
  40. Calculate();
  41. float result = mCoefs[0];
  42. int n = (int) mInputPoints.size();
  43. for (int i = 1; i < n; i++)
  44. {
  45. float add = mCoefs[i];
  46. for (int j = 0; j < i; j++)
  47. add *= (x - mInputPoints[j].x);
  48. result += add;
  49. }
  50. return result;
  51. }