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?