Вот входные данные:
Контрольные точки
Код: Выделить всё
xcp = {
5, 5, 16, 31, 22, 33, 44, 42, 51, 50, 59};
ycp = {
27, 12, 29, 18, 9, 9, 20, 29, 28, 10, 10};
Код: Выделить всё
vknot = {
0.0, 0.0, 0.0, 0.0,
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
8.0, 8.0, 8.0, 8.0};
Я попробовал реализовать описанный алгоритм здесь. https://mathworld.wolfram.com/B-Spline.html
Вот мой код
Код: Выделить всё
#include
#include
#include
#include
#include
#include
class CSpline
{
public:
int m_ControlPointCount;
std::vector xcp, ycp, vknot;
double current;
void generateInput();
bool getNextPoint(int &xp, int &yp);
private:
double BSN(int i, int j, double t);
};
CSpline theSpline;
void CSpline::generateInput()
{
vknot = {
0.0,
0.0,
0.0,
0.0,
1.0,
2.0,
3.0,
4.0,
5.0,
6.0,
7.0,
8.0,
8.0,
8.0,
8.0};
if (fabs(1 - vknot.back()) > 0.01)
{
// normalize knot values
for (double &v : vknot)
v /= vknot.back();
}
xcp = {
5, 5, 16, 31, 22, 33, 44, 42, 51, 50, 59};
ycp = {
27, 12, 29, 18, 9, 9, 20, 29, 28, 10, 10};
current = 0;
}
bool CSpline::getNextPoint(int &xp, int &yp)
{
// number of points drawn along curve
const int Ndiv = 100;
if (current == Ndiv)
return false;
double t = current / Ndiv;
int degree = vknot.size() - xcp.size() - 1;
double x,y;
x = y = 0;
for (int i = 0; i < xcp.size(); i++)
{
double N = BSN(i, degree, t);
x += xcp[i] * N;
y += ycp[i] * N;
}
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78540699/generating-a-clamped-b-spline[/url]