Bezier Curves

Bezier curves are a technique for drawing curved lines around specific points. While not technically a method for procedurally generating content, it is quite useful in displaying such content.

Bezier Curve Algorithm
Two algorithms are presented here: the original version, with a simple but inefficient implementation, and a version known as de Casteljau's algorithm.

Bezier Curve Algorithm
Given a list of x-y coordinates of length n, do the following: for a series of numbers t from 0 to 1: set final x and final y to 0 for coord in position k in the list of coordinates: add to final x coordinate the result of this equation: coord x*(n!/k!*(n-k)!)*(1-t)^k add to final y coordinate the result of this equation: coord y*(n!/k!*(n-k)!)*(1-t)^k draw a point at coordinate of (final x, final y) and connect it to the previous point

Bezier Curve Algorithm Explained
The algorithm takes a series of points on the x-y graph. This version of the algorithm displays it as a bunch of line segments, the exact number determined by the number of subdivision the coder chooses. For each iteration of t, the weight of each coordinate is added to find the point.

The most complicated part of the algorithm is this: (n!/i!*(n-i)!) This is called a binomial and usually expressed as (n k) where n is located above the k. It is usually read out loud as "n choose k," and often appears in the branch of math known as combinatorics or discrete math.

When all the sums are finished, the final coordinate is the next point along the line. Draw this point, then connect it to the previous point (if there are any).

de Casteljau's Algorithm
connect the each point of the curve with a normal, straight line to the next point choose a number t between 0 and 1 on each line, mark a point at distance t along that line. For example if t = .2, then the point is placed .2 or 20% along the line. draw lines between the newly created points repeat the process of creating points and connecting lines until there is only one line left.

de Casteljau's Algorithm Explained
The algorithm repeatedly places points along existing lines, then connects those points with new lines. Each iteration, there is one fewer lines and one fewer point, until eventually there is only one point. By repeatedly using this algorithm with different, perhaps incrementing, values of t, a series of points along the Bezier curve is created. A smoother curve is created by using a larger number of points.

Code Examples
The code examples are available in the bezier folder in the repository.

Bezier Curve Demo
This demo simply shows a Bezier curve of four points arranged in a roughly W-shaped arrangement.