Weirdly: adv. In a strikingly odd or unusual manner
< Older Blog - The Latest - Pick One - Show All

16-07-2014 Points on a Parabola

You can try it out here with this JavaScript version of the code. A modern-ish browser required, as it uses an HTML5 canvas.

A couple of years ago, I wrote a simple program in C that lets you drag three different points around on the screen. These points would be used to calculate a parabola that they all lie on, and plot that parabola on the screen. The algebra used in working out the function for the parabola is relatively easy to do, and that is the subject of this blog.

As you may recall, a parabola can be represented using a quadratic equation:

y = ax2 + bx + c

The work then is pretty straightforward. We'll take the x and y coordinates of any three points (where no two have the same x value), and plug each pair of values into the equation above. With each of them we can solve for either a, b or c. Once we have any of those three terms calculated, we'll substitute it into one of our other solutions, eliminating that variable. Repeating this process will eliminate all three of them, defining them strictly in terms of our three given points.

The points in this case will be defined as (x1, y1),(x2, y2). and (x3, y3). We'll start with the simplest one, solving for c:

y1 = ax12 + bx1 + c
c = y1 - ax12 - bx1

Next we'll solve for b, by using (x2, y2) for our x and y values, while replacing c with the equation above

y2 = ax22 + bx2 + c
y2 = ax22 + bx2 + (y1 - ax12 - bx1)
bx2 - bx1 = y2 - ax22 + ax12 - y1
b (x2 - x1) = y2 - a(x22 - x12) - y1
b (x2 - x1) = y2 - y1 - a(x2 - x1)(x2 + x1)
b =
y2 - y1
x2 - x1
- a(x2 + x1)

Now we have a solution for c given b and a, and a solution for b given a. Next we'll find our solution for a, and put the whole thing together.

y3 = ax32 + bx3 + c
y3 = ax32 + bx3 + (y1 - ax12 - bx1)
y3 = ax32 - ax12 + bx3 - bx1 + y1
a(x32 - x12) + b(x3 - x1) = y3 - y1
a(x3 - x1)(x3 + x1) + b(x3 - x1) = y3 - y1
a(x3 + x1) + b =
y3 - y1
x3 - x1
a(x3 + x1) +
y2 - y1
x2 - x1
- a(x2 + x1)
=
y3 - y1
x3 - x1
a(x3 + x1 - x2 - x1) =
y3 - y1
x3 - x1
-
y2 - y1
x2 - x1
a(x3 - x2) =
(y3 - y1)(x2 - x1) - (y2 - y1)(x3 - x1)
(x3 - x1)(x2 - x1)
a =
(y3 - y1)(x2 - x1) - (y2 - y1)(x3 - x1)
(x3 - x1)(x2 - x1)(x3 - x2)
a =
x2y3 - x1y3 - x2y1 + x1y1 - x3y2 + x1y2 + x3y1 - x1y1
x32x2 - x3x22 - x32x1 + x1x2x3 - x1x2x3 + x1x22 + x12x3 - x12x2
a =
y1(x3 - x2) + y2(x1 - x3) + y3(x2 - x1)
x12(x3 - x2) + x22(x1 - x3) + x32(x2 - x1)

And now we have our three coefficients, and can use our original quadratic equation to calculate the y co-ordinate of any point on the curve given its x co-ordinate.