# The Math and ActionScript of Curves: Drawing Quadratic and Cubic Curves

This post is part of a series called You Do The Math.
Understanding Affine Transformations With Matrix Mathematics
The Math and ActionScript of Curves: Roots

We see lines used in a lot of scenarios. Curves are also used, although perhaps not as frequently - but that doesn't undermine their importance! In this tutorial we shall take a closer look at curves, particularly the quadratic and cubic curve, along with some of their commonly used mathematical features.

## Final Result Preview

Let's take a look at the final result we will be working towards. Drag the red dots and see the gradients change in position.

And here's another demo, using cubic curves, without the gradients:

## Step 1: Curves

Quadratic and cubic will be featured in each of these sections. So let's first look at the equation of curves. These equations are written in polynomial form, starting with the term of highest degree. The first one is quadratic equation (highest degree is 2); the second is cubic equation (highest degree is 3).
$f(x) = Ax^2 + Bx + C\ ... (eq\ 1)$
$g(x) = Ax^3 + Bx^2 + Cx + D\ ... (eq\ 2)$

Note that A, B, C and D are real numbers. So now that we are aquainted with it, let's try to visualise it. Graphing curves will be our next attempt.

## Step 2: Graphing Curves

First, let's graph a quadratic curve. I'm sure all readers have graphed quadratic curve in high school math class, but just to refresh your memory, I present graphs below. They are placed side by side to ease comparison.

• Left graph is using Cartesian coordinate space
• Right graph is using Flash coordinate space

The obvious difference is the inverted y-axis on Flash coordinate space. They look simple overall, right? Okay, now we're ready to plot onto Flash coordinate space.

To position quadratic curves at the right spot, we need to understand their corresponding equations. The curve drawn is really dependant on the equation's coefficients (for the case of quadratic, those are A, B and C).

I've included a Flash presentation below so you can easily tweak these coefficients and get immediate feedback.

To study the effects of individual coefficients on the overall curve, I suggest following the steps below to experiment with the Flash presentation above.

1. While setting A and B to 0, tweak the value of C to both positive and negative values. You'll see the line's height change.
2. Now tweak the value of B between positive and negative values. Observe what happens to gradient of line.
3. Now tweak the value of A between positive and negative values, and compare the results.
4. Then tweak B between being positive and negative again. Observe the curve's always cutting through the origin.
5. Finally tweak C. Observe the whole curve shift along the y-axis.

Another interesting observation is that throughout the second and third steps of the above, the point of inflection (i.e. the turning point) stays at the same point on the y-axis.

## Step 4: Alternative Equation One

You quickly see that positioning a curve is somewhat difficult. The equation used is impractical if we want to, say, locate the coordinates of the lowest point on a curve.

Solution? We'll rewrite the equation into a desired form. Check out the following equation:

$f(x) = P(x+Q)^2+R$

It's still a quadratic equation, but it's taken another form. Now we can easily control the minimum and maximum points on the curve. In the previous Flash presentation, click on button "Approach 1" on the top right and play with the new values.

Here's a brief explanation of the coefficients' roles:

 Coefficient Role P Control the curve's steepness. Q Control displacement of curve's turning point along x-axis. R Control displacement of curve's turning point along y-axis.

Nonetheless, it's still a difficult task to make the curve pass through a given set of points. We'd have to rigorously pre-calculate on paper before translating it to code.

Fortunately, there is a better solution. But before going through it, let's have a look at the ActionScript implementation as of now.

## Step 5: ActionScript Implementation

Here are the equations written as ActionScript functions (check Graphing.as in the source download).

And here's an implementation of the drawing method using Graphics.drawPath(). Just a note that all curves in this article are drawn in similar fashion.

First the variables...

Now the y-positions, calculated based on the x-positions and the given coefficients.

(Confused about the >> operator? Take a look at this tutorial.)

## Step 6: Alternative Equation Two

Suppose we're given three points that the quadratic curve must cross through; how do we form the corresponding equation? More specifically, how can we determine the coefficient values of the equation? Linear algebra comes to the rescue. Let's analyse this problem.

We know that quadratic equations always take form as written in eq. 1 in Step 1.

$f(x) = Ax^2 + Bx + C\ ... (eq\ 1)$

Since all three coordinates given are lying on the same curve, they must each satisfy this equation, with the same coefficients as the equation of the curve that we are looking for. Let's write this down in equation form.

Given three coodinates:

• $$S\ \left(S_x,\ S_y\right)$$
• $$T\ \left(T_x,\ T_y\right)$$
• $$U\ \left(U_x,\ U_y\right)$$

Substitute these values into (eq 1). Note that A, B, C are unknowns at the moment.

$f(x) = Ax^2 + Bx + C\ ... (eq\ 1)$

• $$S_y = A\left(S_x\right)^2 + B\left(S_x\right) + C\$$
• $$T_y = A\left(T_x\right)^2 + B\left(T_x\right) + C\$$
• $$U_y = A\left(U_x\right)^2 + B\left(U_x\right) + C\$$

Now, rewrite in matrix form. Take note that A, B, C are the unknowns we are solving for.

$\begin{bmatrix}S_y \\T_y \\U_y\end{bmatrix} = \begin{bmatrix} \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^2 & \left(U_x\right) & 1\end{bmatrix} \begin{bmatrix}A \\B \\C\end{bmatrix} \$/extract_itex] $\begin{bmatrix} \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^2 & \left(U_x\right) & 1\end{bmatrix}^{-1} \begin{bmatrix}S_y \\T_y \\U_y\end{bmatrix} = \begin{bmatrix} \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^2 & \left(U_x\right) & 1\end{bmatrix}^{-1} \begin{bmatrix} \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^2 & \left(U_x\right) & 1\end{bmatrix} \begin{bmatrix}A \\B \\C\end{bmatrix} \\$ $\begin{bmatrix} \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^2 & \left(U_x\right) & 1\end{bmatrix}^{-1} \begin{bmatrix}S_y \\T_y \\U_y\end{bmatrix} = I \begin{bmatrix}A \\B \\C\end{bmatrix} \\ K^{-1}J = L$ Of course we can use simultaneous equations instead, but I prefer using matrices because it's simpler. (Editor's note: as long as you understand matrices, that is!) We'll get the inverse of K and multiply by the J matrix to get L. After we have successfully solved for A, B, C, we'll just substitute into the quadratic equation. Thus, we'll have a quadratic curve that passes through all three points. ## Step 7: Importing Coral As mentioned in the previous step, we need to perform a 3x3 matrix inversion and multiplication. ActionScript's flash.geom.matrix class won't be able to help in this. Of course, we have a choice to utilise flash.geom.Matrix3D, class but I prefer the Coral library because I can pry into these custom classes and examine what's happening under the hood. I personally find this very useful whenever at doubt on proper use of commands even after reading the API documentation. So download and place the unzipped Coral files into your project source folder. ## Step 8: ActionScript Implementation Here's a sample of the result. Try to reposition the red dots and see the quadratic curve redrawn to cross through all three points. ## Step 9: Implementation Explained You can find the full script in Draw_curve.as. The following ActionScript is just to enable mouse controls on the little dots. The core lies in the redraw function. I've highlighted the matrix operations and the quadratic function for the redraw process. So you can see that the matrix K was initialised and inverted before being appended onto matrix J. The append() function multiplies the current matrix, J, with the input matrix, K, placed to its left. Another noteworthy detail is that we don't utilise all the rows and columns in K and J matrices. However since matrix inversion can only happen with a square matrix, we need to fill in the 4th row, 4th column element of K with 1. (There's no need to do this for J because we don't need its inversion in our calculation.) Thus, you can see all the other elements are 0 except for the first column. ## Step 10: Graphing Cubic Curve So that's all for drawing quadratic curves. Let's move on to cubic curves. Again, we'll have a little revision of graphing these curves. Check out the following image: When you compare this curve to that of quadratic, you will notice that it is steeper, and that a portion of the curve is below the x-axis. One half is mirrored vertically, compared to a quadratic. ## Step 11: Cubic Coefficients I've included the following Flash presentation to let you experiment with the coefficients of a cubic equation. Try tweaking the value of A from positive to negative and observe the difference in the curve produced. ## Step 12: ActionScript Implementation Here's the important section of the implementation of the graphing above: Again, it's difficult to position the cubic curve according to a set of points it crosses through. Once again, we refer to linear algebra for an alternative. ## Step 13: Alternative Method We know from Step 6 that the coefficients of a quadratic equation can be calculated based on three given points, and the curve drawn from it will cross through those points. A similar approach can be performed with any four given points to obtain a cubic equation: • $$S\ \left(S_x,\ S_y\right)$$ • $$T\ \left(T_x,\ T_y\right)$$ • $$U\ \left(U_x,\ U_y\right)$$ • $$V\ \left(V_x,\ V_y\right)$$ Substitute these coordinates into (eq 2). Note that A, B, C, D are unknowns. \[g(x) = Ax^3 + Bx^2 + Cx + D\ ... (eq\ 2)$

• $$S_y = A\left(S_x\right)^3 + B\left(S_x\right)^2 + C\left(S_x\right) + D$$
• $$T_y = A\left(T_x\right)^3 + B\left(T_x\right)^2 + C\left(T_x\right) + D$$
• $$U_y = A\left(U_x\right)^3 + B\left(U_x\right)^2 + C\left(U_x\right) + D$$
• $$V_y = A\left(V_x\right)^3 + B\left(V_x\right)^2 + C\left(V_x\right) + D$$

But now we'll deal with a 4x4 matrix instead of 3x3 matrix:

$$\begin{bmatrix}S_y \\T_y \\U_y \\V_y\end{bmatrix} = \begin{bmatrix} \left(S_x\right)^3 & \left(S_x\right)^2 & \left(S_x\right) & 1\\ \left(T_x\right)^3 & \left(T_x\right)^2 & \left(T_x\right) & 1\\ \left(U_x\right)^3 & \left(U_x\right)^2 & \left(U_x\right) & 1\\ \left(V_x\right)^3 & \left(V_x\right)^2 & \left(V_x\right) & 1\end{bmatrix} \begin{bmatrix}A \\B \\C \\D\end{bmatrix} \\ P = QR \\ Q^{-1}P = Q^{-1}QR \\ Q^{-1}P = IR\\ Q^{-1}P = R$$

Now we will utilise all elements in the 4x4 matrix for Q and the whole first column for P. Then Q is inversed and applied to P.

## Step 14: ActionScript Implementation

Again, we set up the mouse controls to allow dragging of those points. When any of those points are being dragged, recalculation and redrawing of the curve constantly happen.

redraw is the crucial function where everything happened.

Finally, let's look at the product. Click and move the red dots to see cubic curve drawn to pass through all those points.

## Step 15: Polynomials of Higher Degree

We just gone through drawing polynomials of degree 2 and 3 (quadratic and cubic). From our experience, we can predict that calculation for polynomial of degree 4 (quintic) will require five points, which will require 5x5 matrix, and so on for polynomials of even higher degrees.

Unfortunately, Coral and flash.geom.Matrix3D only allow for 4x4 matrices, so you'll have write your own class if the need does come. It's seldom required in games, though.

## Step 16: Dividing Regions

Let's try to apply our knowledge to divide regions on our stage. This requires some revision of equation inequalities. Check out the image below.

This image above shows a curve dividing the regions into two:

• Blue region on top, where for each point y is greater than the equation of the curve.
• Red region at bottom, where for each point y is less than the equation of the curve.

It's not hard to grasp this concept. In fact, you have already experimented on this in Step 11 as you tweaked the coefficients of the cubic formula. Imagine, in the coordinate system, that there is an infinite number of curves, all differentiated by just a slight change in D:

## Step 17: ActionScript Implementation

So here's the sample of output for quadratic curve. You can try to move the red dot around and see the regions coloured.

Here's the important ActionScript snippet. Check out the full script in Region_Curve.as

Here's the sample with regard to cubic curve.

And the implementation that comes with it. Again, full script's in Region_Curve2.as

## Step 18: Variations

How about some tweaks to change the color across different curves? Again, mouse click on the red dots and see the gradient changes across the screen.

## Step 19: ActionScript Implementation

Here's the important ActionScript snippet extracted from Region_Curve3.as. First of all we'll want to find out the maximum and minimum offset from the original curve.

Once done, we'll apply it to colouring the individual dots.

## Conclusion

So that all for the drawing of curves. Next up, finding roots of a quadratic and cubic curve. Thanks for reading. Do share if you see some real life applications that takes advantage of this tutorial.