1

I am trying to draw a curve with B-spline. I did my research about what is B-spline and how can I use it in a program algorithm. After all that stuff, I finally find a code to make this right in Stack Overflow. I made some changes on this code and try to use in my program. It works but I have two problems with that.

Firstly the curve is in the right shape but not in the right position. It's like 20-40 pixel different than should to be.

Secondly in my function last part, I am dividing the two result of x and y to a number but it (divide number) seems like have to change for all the circumstances.

And finally, it's working for 6 coordinates as you can see.

How can I bind the number of coordinates to divide the number and fix the flip at the spline?

PS: I need to write that code with C

Here is my code's functions :

1-That's my B-spline calculate function:

void BSplineCurve(const Dot& point1,
    const Dot& point2,
    const Dot& point3,
    const Dot& point4,
    Dot& result,
    const double t)
{

    const double t2 = t * t;
    const double t3 = t2 * t;
    const double mt = 1.0 - t;
    const double mt3 = mt * mt * mt;

    const double bi3 = mt3;
    const double bi2 = 3 * t3 - 6 * t2 + 4;
    const double bi1 = -3 * t3 + 3 * t2 + 3 * t + 1;
    const double bi = t3;

    result.x = point1.x * bi3 + point2.x * bi2 + point3.x * bi1 + point4.x * bi;
    result.x /= 4;

    result.y = point1.y * bi3 + point2.y * bi2 + point3.y * bi1 + point4.y * bi;
    result.y /= 4;
}

2- That's my Draw Function :

Dot points[6] = {ControlPoint1, ControlPoint2, ControlPoint3, ControlPoint4, ControlPoint5, 
ControlPoint6}; 
for(double t = 5.9999;t > 2.0; t -= 0.001)
{
    const int start = static_cast<int>(t)+1;
    BSplineCurve(points[start -3 ],
                 points[start - 2], 
                 points[start - 1], 
                 points[start ],
                 DrawCurve, 
                 start - t);
    Draw1Dot(DrawCurve,points[0],distanceToEdges);}

3- And finally my Draw pixel function :

void Draw1Dot(Dot Koor, Dot mesafe, int ortala)
{

    putpixel(mesafe.x + Koor.x + ortala, mesafe.y + Koor.y + ortala, 3);
}

Can you help me understand what I'm doing wrong?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • Note: the [tag:graphics] tag is for graphics in general. You're referring to an ancient graphics library called [tag:BGI]. – Botje Nov 02 '20 at 12:37
  • Universal calculations: https://godbolt.org/z/aK9e4r – Marek R Nov 02 '20 at 12:52
  • @Botje Yes mb sorry thank you for correcting.@MarekR Thank you for your answer but I tried to understand that C++ code. Actually It's hard to me get that because of my missing information about C++ but I will try to sort out. Thank you both again. – Ali Barış Zengin Nov 02 '20 at 13:18
  • Not directly an answer but reading these might help: https://gitlab.com/Makogan/mathproofs/-/blob/master/B-splines/b-splines.pdf – Makogan Nov 03 '20 at 19:46

0 Answers0