c - n-th order Bezier Curves? -
i've managed implement quadratic , cubic bezier curves.they pretty straightforward since have formula. want represent n-th order bezier curve using generalization:
where
and
i'm using bitmap library render output, here code:
// binomialcoef(n, k) = (factorial(n) / (factorial(k) * factorial(n- k))) unsigned int binomialcoef(unsigned int n, const unsigned int k) { unsigned int r = 1; if(k > n) return 0; for(unsigned int d = 1; d <= k; d++) { r *= n--; r /= d; } return r; } void nbeziercurve(bitmap* obj, const point* p, const unsigned int nbpoint, float steps, const unsigned char red, const unsigned char green, const unsigned char blue) { int bx1 = p[0].x; int by1 = p[0].y; int bx2; int by2; steps = 1 / steps; for(float = 0; < 1; += steps) { bx2 = by2 = 0; for(int j = 0; (unsigned int)j < nbpoint; j++) { bx2 += (int)(binomialcoef(nbpoint, j) * pow(1 - i, (float)nbpoint - j) * pow(i, j) * p[j].x); by2 += (int)(binomialcoef(nbpoint, j) * pow(1 - i, (float)nbpoint - j) * pow(i, j) * p[j].y); } bresenhamline(obj, bx1, by1, bx2, by2, red, green, blue); bx1 = bx2; by1 = by2; } // curve must end on last anchor point bresenhamline(obj, bx1, by1, p[nbpoint - 1].x, p[nbpoint - 1].y, red, green, blue); }
here's set of points render:
point ncurv[] = { 20, 200, 70, 300, 200, 400, 250, 200 };
and here's output:
the red curve cubic bezier. blue 1 supposed 4th order bezier, same cubic bezier, in case, not same ?!
edit : forgot note bottom left point (0, 0)
the sum in formula...
...runs 0 n, ie n-th order bezier need n+1 points.
you have 4 points, you're drawing 3rd-order bezier.
the error in code here:
for(int j = 0; (unsigned int)j < nbpoint; j++)
it should be:
for(int j = 0; (unsigned int)j <= nbpoint; j++)
otherwise you're iterating 0 n-1.
edit:
out of interest, shape getting same if missing (5th) point @ (0,0), since that's point contribute nothing sum...
Comments
Post a Comment