13,457,717 members
alternative version

#### Stats

275.4K views
97 bookmarked
Posted 14 Apr 2008

, 14 Apr 2008
A simple implementation of the famous Bezier curves in C#. Easy to understand.

## Introduction

Bezier curves are the most fundamental curves, used generally in computer graphics and image processing. These curves are mainly used in interpolation, approximation, curve fitting, and object representation. In this article, I will demonstrate, in a very simple and straightforward way, how one can construct these curves and make use of them.

## Background

Bezier curves are parametric curves which are pretty much customizable and smooth. They are well suited for many applications. They were named after Pierre Bézier, a French mathematician and engineer who developed this method of computer drawing in the late 1960s while working for the car manufacturer Renault. People say that at the same time the same development took place during the research of Ford. There is still a confusion about who found it first.

Because of my imaging background, my article will mainly focus on interpolation and curve fitting. In interpolation, what one would simply like to do is to find unknown points using known values. This way, a discrete case can be represented with a more continuous structure, and we can have a well defined curve for missing points. The curve is initialized with certain data points, and it tries to generate new ones that are approximating (or interpolating) the old values.

##### Constructive Bezier Curve Algorithm

Consider the n+1 points P0,…,Pn and connect the points into a polyline we will denote hereafter as the control polygon.

Given points Pi, i = 0,...,n, our goal is to determine a curve g (t), for all values t Î [0,1]. The idea is demonstrated below:

##### Basic Algorithm

The objective here is to find points in the middle of two nearby points and iterate this until we have no more iterations. The new values of points will give us the curve. The famous Bezier equation is the exact formulation of this idea. Here is the algorithm:

Step 1: Select a value t Î [0,1]. This value remains constant for the rest of the steps.

Step 2: Set Pi[0] (t) = Pi, for i = 0,...,n.

Step 3: For j= 0,...,n, set for i = j,...,n.

Step 4: g (t) = Pn[n] (t)

##### Special & General Cases

Now, I will give formulas for common, special cases that can be helpful in certain applications. The code of the article does not demonstrate any of them, but it uses the generalized formula. So, let me start with the generalized formula:

For the sake of simplicity and convention used in this article and code, it is better to represent this formula as:

What this equation tells us is nothing but the formulation of the above algorithm (the mid-point iterations). It is very important in the sense that a whole algorithm could be summarized into a formula and a straightforward implementation would yield correct results. Here, n denotes the number of points and P denotes the points themselves. The factorial coefficients of the points are simply called the Bernstein basis functions, because of the name of the founder.

Here are the special cases:

Linear Bezier:

Cubic Bezier:

## Understanding and Using the Code

This is the function, doing all the work. I think it is very short and very easy. Because we are dealing only with 2D curves, we have points in X and Y coordinates. The function simply calculates the Bezier points.

```public void Bezier2D(double[] b, int cpts, double[] p)
{
int npts = (b.Length) / 2;
int icount, jcount;
double step, t;

// Calculate points on curve

icount = 0;
t = 0;
step = (double)1.0 / (cpts - 1);

for (int i1 = 0; i1 != cpts; i1++)
{
if ((1.0 - t) < 5e-6)
t = 1.0;

jcount = 0;
p[icount] = 0.0;
p[icount + 1] = 0.0;
for (int i = 0; i != npts; i++)
{
double basis = Bernstein(npts - 1, i, t);
p[icount] += basis * b[jcount];
p[icount + 1] += basis * b[jcount + 1];
jcount = jcount +2;
}

icount += 2;
t += step;
}
}```

The rest of the functions are only helper functions taking part in factorial calculations and basis function calculations, which I believe are pretty obvious. To properly use this function, give it a set of points in the format: XYXYXYXYXYXYXYXYXYXY.... coordinates and how many points you would like to calculate on the curve. The function will fill the `p` array with the path points.

## Points of Interest

Because of the limitations of factorial calculations, the code could only calculates curves up to 32 points. More complicated structures are generally represented by a combination of these curves (as in Adobe Photoshop, Illustrator, and Flash - path tool).

Even though GDI has a built-in Bezier curve calculation function, it is never good for learning to use built-in libraries. You won't always have GDI to do things for you! At some place, some time, you may have to implement it, and I think by now, you should have a rough idea of how these curves work.

## Share

 CEO Gravi Information Technologies and Consultancy Ltd Turkey
Currently, also an MSc. student in Technical University of Munich, I develop practical application in computer vision for more than 5 years. I design real-time solutions to industrial and practical vision problems, both 3D and 2D. Very interested in developing algorithms in C relating math and vision.

"Great minds never die, they just tend to infinity..."

## You may also be interested in...

 Pro

 FirstPrev Next
 Re: Bernstein polynomials fresi13-Apr-10 14:07 fresi 13-Apr-10 14:07
 Re: Bernstein polynomials Tolga Birdal21-Sep-14 7:43 Tolga Birdal 21-Sep-14 7:43
 Thanks! Grummel24-Apr-08 8:09 Grummel 24-Apr-08 8:09
 Re: Thanks! Tolga Birdal24-Apr-08 9:02 Tolga Birdal 24-Apr-08 9:02
 Re: Thanks! Grommel27-Apr-08 6:05 Grommel 27-Apr-08 6:05
 Re: Thanks! Tolga Birdal27-Apr-08 7:40 Tolga Birdal 27-Apr-08 7:40
 Re: Thanks! Grommel28-Apr-08 3:04 Grommel 28-Apr-08 3:04
 Re: Thanks! a-nema19-May-10 4:59 a-nema 19-May-10 4:59
 thanks to the author! here is my version, adopted for my own needs: ```
using System;
using System.Collections.Generic;
using System.Drawing;

namespace Curves
{
public static class BezierCurve
{
private static double[] FactorialLookup = new double[]
{
1.0,
1.0,
2.0,
6.0,
24.0,
120.0,
720.0,
5040.0,
40320.0,
362880.0,
3628800.0,
39916800.0,
479001600.0,
6227020800.0,
87178291200.0,
1307674368000.0,
20922789888000.0,
355687428096000.0,
6402373705728000.0,
121645100408832000.0,
2432902008176640000.0,
51090942171709440000.0,
1124000727777607680000.0,
25852016738884976640000.0,
620448401733239439360000.0,
15511210043330985984000000.0,
403291461126605635584000000.0,
10888869450418352160768000000.0,
304888344611713860501504000000.0,
8841761993739701954543616000000.0,
265252859812191058636308480000000.0,
8222838654177922817725562880000000.0,
263130836933693530167218012160000000.0
};

// just check if n is appropriate, then return the result
//private static double factorial(int n)
//{
// if (n < 0) { throw new Exception("n is less than 0"); }
// if (n > 32) { throw new Exception("n is greater than 32"); }
// return FactorialLookup[n]; /* returns the value n! as a SUMORealing point number */
//}

private static double Ni(int n, int i)
{
double ni;
double a1 = FactorialLookup[n];
double a2 = FactorialLookup[i];
double a3 = FactorialLookup[n - i];
//double a1 = factorial(n);
//double a2 = factorial(i);
//double a3 = factorial(n - i);
ni = a1 / (a2 * a3);
return ni;
}

// Calculate Bernstein basis
private static double Bernstein(int n, int i, double t)
{
double basis;
double ti; /* t^i */
double tni; /* (1 - t)^i */

/* Prevent problems with pow */

if (t == 0.0 && i == 0)
ti = 1.0;
else
ti = Math.Pow(t, i);

if (n == i && t == 1.0)
tni = 1.0;
else
tni = Math.Pow((1 - t), (n - i));

//Bernstein basis
basis = Ni(n, i) * ti * tni;
return basis;
}

public static List Bezier2D(List bezierPoints, int numOfPointsToCalculate)
{
List calculatedPoints = new List();
double t = 0;
double step = 1.0 / (numOfPointsToCalculate - 1);
int numOfKnots = bezierPoints.Count - 1;

for (int i1 = 0; i1 != numOfPointsToCalculate; i1++)
{
if ((1.0 - t) < 5e-6)
{
t = 1.0;
}

double x = 0.0;
double y = 0.0;
int i = 0;
foreach (PointF bezPoint in bezierPoints)
{
double basis = Bernstein(numOfKnots, i, t);
x += basis * (double)bezPoint.X;
y += basis * (double)bezPoint.Y;
i++;
}

t += step;
}
return calculatedPoints;
}
}
}
```
 I think you have an understanding gap Fresric Bouemont14-Apr-08 23:47 Fresric Bouemont 14-Apr-08 23:47
 Re: I think you have an understanding gap Tolga Birdal15-Apr-08 2:27 Tolga Birdal 15-Apr-08 2:27
 Re: I think you have an understanding gap Fresric Bouemont15-Apr-08 23:57 Fresric Bouemont 15-Apr-08 23:57
 Re: I think you have an understanding gap Tolga Birdal16-Apr-08 4:04 Tolga Birdal 16-Apr-08 4:04
 Re: I think you have an understanding gap Fresric Bouemont16-Apr-08 7:52 Fresric Bouemont 16-Apr-08 7:52
 Re: I think you have an understanding gap Tolga Birdal16-Apr-08 9:26 Tolga Birdal 16-Apr-08 9:26
 Re: I think you have an understanding gap Fresric Bouemont16-Apr-08 11:12 Fresric Bouemont 16-Apr-08 11:12
 Re: I think you have an understanding gap Tolga Birdal16-Apr-08 9:35 Tolga Birdal 16-Apr-08 9:35
 Re: I think you have an understanding gap justinctree9-Jun-10 7:37 justinctree 9-Jun-10 7:37
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Mar-18 13:29 Refresh « Prev12