mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 03:28:20 +02:00
67 lines
1 KiB
C++
67 lines
1 KiB
C++
#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;
|
|
}
|
|
|