naman1901
naman1901

Reputation: 305

B-Splines in C++

I'm trying to write a program to generate a curve in C++ to plot a B-Spline curve. This is what my code looks like.

void drawBSplineCurve(vector<point> poly)
{
    int n, d;
    cout << "Enter degree of curve: ";
    cin >> d;
    n = poly.size();
    vector<double> uVec;
    int i;
    for(i=0;i<n+d;i++)
    {
        uVec.push_back(((double)i)/(n+d-1));
    }
    double x, y, basis, u;
    for(u=0;u<=1;u+=0.0001)
    {
        x = 0;
        y = 0;
        for(i=0;i<poly.size();i++)
        {
            basis = blend(uVec, u, i, d);
            x += basis*poly[i].x;
            y += basis*poly[i].y;
        }
        putpixel(roundOff(x), roundOff(y), YELLOW);
    }
}

double blend(vector<double> &uVec, double u, int k, int d)
{
    if(d==1)
    {
        if(uVec[k]<=u && u<uVec[k+1])
            return 1;
        return 0;
    }
    double b;
    b = ((u-uVec[k])/(uVec[k+d-1]-uVec[k])*blend(uVec, u, k, d-1)) + ((uVec[k+d]-u)/(uVec[k+d]-uVec[k+1])*blend(uVec, u, k+1, d-1));
    return b;
}

However, as you can see from my output, the curve, for some reason, tends to start and end at the origin (y-axis is inverted). Any help on the reason for this would be appreciated. Thanks :D

Output

Upvotes: 2

Views: 7773

Answers (2)

SoLuTan
SoLuTan

Reputation: 1

So after investing way more time than I should have, I finally found the answer in a line in my textbook that I missed. Apparently, the curve is defined only for values of u between uVec[d-1] and uVec[n].

It means that mistake is in function "blend" Conditinon (uVec[k]<=u && u<uVec[k+1])

Upvotes: -2

naman1901
naman1901

Reputation: 305

So after investing way more time than I should have, I finally found the answer in a line in my textbook that I missed. Apparently, the curve is defined only for values of u between uVec[d-1] and uVec[n].

Upvotes: 2

Related Questions