# Bonus: Equations from de Casteljau's algorithm

Challenge question: can you work out the equations for n-degree curves generated by de Casteljau's algorithm?

## Parametric equation for a line

In the first step of de Casteljau's algorithm we define a point along a line in terms of $t$. For example, if we have a line between two points, $\blue{A}$ and $\blue{B}$, then we can define a point, $P(t)$ on that line.
The equation for the point is:
$P(t) = (1- t)\blue{A} + t\blue{B}$
A line between points A and B
As $t$ goes from $0$ to $1$, $P(t)$ traces out the line from $\blue{A}$ and $\blue{B}$. The equation is linear, so the line can be considered a degree $1$ curve.

### Degree $2$ curves

When we create a degree $2$ curve (a parabola), we use three points, $\blue{A}$, $\blue{B}$, and $\blue{C}$
A parabolic arc defined by points A, B and C
Now we get this equation for a point on the curve:
$P(t) = (1- t)^2\blue{A} + 2(1- t)t\blue{B} + t^2\blue{C}$

### Degree $3$ curves

If we create a degree $3$ curve using four points, $\blue{A}$, $\blue{B}$, $\blue{C}$, and $\blue{D}$, is the equation for a point on the curve in terms of $\blue{A}$, $\blue{B}$, $\blue{C}$, and $\blue{D}$?
$P(t) =$

### Degree $4$ curves

What about if we create a degree $4$ curve using five points, $\blue{A}$, $\blue{B}$, $\blue{C}$, $\blue{D}$, and $\blue{E}$?
$P(t) =$

### Degree $n$ curves

Now let's see if we can spot any patterns in these equations that will allow us to find a general equation that uses $n + 1$ points, $\blue{A_0}, \blue{A_1}, ..., \blue{A_{n-1}}, \blue{A_n}$, to define an $n$ degree curve.
Look at the first term in each of the above equations and see if you can spot a pattern.
What would be the coefficient for $\blue{A_0}$ in an $n$ degree curve?

Look at the last term in each of the above equations and see if you can spot a pattern.
What would be the coefficient for $\blue{A_n}$ in an $n$ degree curve?

Now, the hardest part: look at the remaining terms in each of the above equations. Notice that each term includes:
1. a constant
2. $(1 - t)$ raised to a power
3. $t$ raised to a power
For example, for a degree $2$ curve, the $\blue{A_1}$ term is $2(1 - t)t$, so the constant term is $2$, the exponent on $(1 - t)$ is $1$, and the exponent on $t$ is $1$.
In the coefficient for the $\blue{A_i}$ term in an equation for an $n$ degree curve:
What is the exponent on $(1 - t)$?

What is the exponent on $t$?

### Extra Super Bonus Challenge

Can you find a formula for the constant term for $\blue{A_i}$? Once you have done that, can you combine all these parts into an equation for $P(t)$ for an $n$ degree curve?
For the constant term, have a look at the last video in the Crowds topic which covers binomial coefficients.
The constant term is a binomial coefficient $\binom{n}{i}$, which can be calculated with:
$\dfrac{n!}{i!(n - i)!}$
So the complete equation for the coefficient of $\blue{A_i}$ is:
$\dfrac{n!}{i!(n - i)!}(1-t)^{n-i}t^i$
Note that this formula works for all the terms, including $\blue{A_0}$ and $\blue{A_n}$.
So the equation for $P(t)$ for any $n$ degree curve is:
$P(t) = \sum \limits_{i = 0}^n \dfrac{n!}{i!(n - i)!}(1-t)^{n-i}t^i\blue{A_i}$