Saturday, June 16, 2012

The Math and ActionScript of Curves: Roots

In the first tutorial of this series, we took a look at drawing curves using equations and AS3. Now, we’re going to tackle solving those equations to find the roots of a curve – that is, the places where the curve crosses a given straight line. We can use this to predict collisions with curved surfaces, and to avoid “tunnelling” in Flash games.

Step 1: Quadratic Roots

First, time for some quick math revision. In this tutorial, we’ll just accept and apply the methods we’ll use, but interested readers can refer to Wikipedia’s page on quadratic equations for information about the mathematical deriviations.
So \(f(x)\) is a quadratic function. If (f(x)) is equivalent to 0, (x) can be obtained by this formula:
[Given f(x) = ax^2+bx+c, ] [f(x) = 0, x = frac{-b pm sqrt{b^2 - 4ac}}{2a} ]
(b^2 – 4ac\) is called the discriminant of the formula. If the discriminant is negative, the square root of the discriminant will produce imaginary roots, which we can’t plot. Conversely, if the discriminat is positive, you will have real number roots and you’ll be able to plot them onto the screen.

Step 2: Visualising Quadratic Roots

So what are roots? Well in our context they are nothing more than intersection points between the quadratic curve and a line. For example, suppose we are interested to find the intersection point(s) of the following set of equations:
(
f(x) = ax^2+bx+c
g(x) = 0
)
This is a typical scenario of looking for the intersection point(s) between a quadratic curve and the x-axis (because the x-axis is the line where y==0). Since by definition the intersection point(s) are shared by (f(x)) and (g(x)), we can conclude that (f(x) = g(x)) for the values of x that we are looking for.
Read more: The Math and ActionScript of Curves: Roots

No comments:

Post a Comment