# Drawing Curvy Lines with aplomb – Beziers

I have written plenty about Bezier and other curves and surfaces.  That was largely in the context of 3D printing using OpenScad, or BanateCad.  But now, I’m adding to a general low level graphics library.

Well well, what do we have here.  Bezier curves are one of those constructs where you lay down some ‘control points’ and then draw a line that meanders between them according to some mathematical formula.  In the picture, the green curve is represented by 4 control points, the red one is represented by 5 points, and the blue ones are each represented by 4 points.

How do you construct a Bezier curve?  Well, you don’t need much more than the following code:

```void computeCoefficients(const int n, int * c)
{
int k, i;

for (k = 0; k <= n; k++)
{
// compute n!/(k!(n-k)!)
c[k] = 1;
for (i = n; i >= k + 1; i--)
{
c[k] *= i;
}

for (i = n - k; i >= 2; i--)
{
c[k] /= i;
}
}
}

void computePoint(const float u, Pt3 * pt, const int nControls, const Pt3 *controls, const int * c)
{
int k;
int n = nControls - 1;
float blend;

pt->x = 0.0;	// x
pt->y = 0.0;	// y
pt->z = 0.0;	// z

// Add in influence of each control point
for (k = 0; k < nControls; k++){
blend = c[k] * powf(u, k) *powf(1 - u, n - k);
pt->x += controls[k].x * blend;
pt->y += controls[k].y * blend;
pt->z += controls[k].z * blend;
}
}

void bezier(const Pt3 *controls, const int nControls, const int m, Pt3 * curve)
{
// create space for the coefficients
int * c = (int *)malloc(nControls * sizeof(int));
int i;

computeCoefficients(nControls - 1, c);
for (i = 0; i <= m; i++) {
computePoint(i / (float)m, &curve[i], nControls, controls, c);
}
free(c);
}

```

This is pretty much the same code you would get from any book or tutorial on fundamental computer graphics. It will allow you to calculate a Bezier curve using any number of control points.

Here’s the test case that generated the picture

```typedef struct {
REAL x;
REAL y;
REAL z;
} Pt3;

void polyline(pb_rgba *pb, Pt3 *curve, const int nPts, int color)
{
for (int idx = 0; idx < nPts; idx++) {
raster_rgba_line(pb, curve[idx].x, curve[idx].y, curve[idx + 1].x, curve[idx + 1].y, color);
}
}

void test_bezier()
{

size_t width = 400;
size_t height = 400;
int centerx = width / 2;
int centery = height / 2;
int xsize = (int)(centerx*0.9);
int ysize = (int)(centery*0.9);

pb_rgba pb;
pb_rgba_init(&pb, width, height);

// background color
raster_rgba_rect_fill(&pb, 0, 0, width, height, pLightGray);

// One curve drooping down
Pt3 controls[4] = { { centerx - xsize, centery, 0 }, { centerx, centery + ysize, 0 }, { centerx, centery + ysize, 0 }, { centerx + xsize, centery, 0 } };
int nControls = 4;
int m = 60;
Pt3 curve[100];
bezier(controls, nControls, m, curve);
polyline(&pb, curve, m, pGreen);

// Several curves going up
for (int offset = 0; offset < ysize; offset += 5) {
Pt3 ctrls2[4] = { { centerx - xsize, centery, 0 }, { centerx, centery - offset, 0 }, { centerx, centery - offset, 0 }, { centerx + xsize, centery, 0 } };
bezier(ctrls2, nControls, m, curve);
polyline(&pb, curve, m, pBlue);
}

// one double peak through the middle
Pt3 ctrls3[5] = { { centerx - xsize, centery, 0 }, { centerx-(xsize*0.3f), centery + ysize, 0 }, { centerx, centery - ysize, 0 }, { centerx+(xsize*0.3f), centery + ysize, 0 }, { centerx + xsize, centery, 0 } };
int nctrls = 5;
bezier(ctrls3, nctrls, m, curve);
polyline(&pb, curve, m, pRed);

// Now we have a simple image, so write it to a file
int err = write_PPM("test_bezier.ppm", &pb);
}
```

From here, there are some interesting considerations. For example, you don’t want to calculate the coefficients every single time you draw a single curve. In terms of computer graphics, most Bezier curves consist of 3 or 4 points at most. Ideally, you would calculate the coefficients for those specific curves and store the values statically for later usage. This is what you’d do ideally for a small embedded library. The tradeoff in storage space is well worth the savings in compute time.

Additionally, instead of calculating all the line segments and then storing those values and using a polyline routine to draw things, you’d like want to simply have the bezier routine draw the lines directly. That would cut down on temporary allocations.

At the same time though, you want to retain the ‘computePoint’ function as independent because once you have a Bezier calculation function within the library, you’ll want to use it to do things other than just draw curved lines. Bezier, and it’s corollary Hermite, are good for calculating things like color ramps, motion, and other curvy stuff. This is all of course before you start using splines and nurbs, which are much more involved process.

There you have it, a couple of functions, and suddenly you have Bezier curves based on nothing more than the low level line drawing primitives. At the moment, this code sits in a test file, but soon enough I’ll move it into the graphicc library proper.