IntroductionThere's no doubt that you've by now heard of these great new technologies that deal with curves, and curved surfaces. Quadratic Bézier patches, cubic Bézier curves, non-uniform rational B-Splines (NURBS), and have been trying to make sense of all this madness. Maybe even trying to add one of these technologies into a game or demo that your making. This is article is a great start to unraveling that mystery. In this article, I will explain to you the mathematics behind these Bézier curves, code that will draw a quadratic bézier spline (the simplest flavor of spline), and some techniques you can use to derive ways of making cubic, quartic, quintic, or whatever degree of bézier curve that you feel that you need. Mathematics Behind Bézier SplinesOk, the first thing that we're going to go over is Bernsteins Basis Functions. Ok, I'm going to throw out a formula here, but don't fret. Just look over it and you'll find that it's really quite simple. 1 = t + (1 - t) T can be any number and this equation would still be true. But for our case, we're only interested in numbers between 0 and 1. Ok, so now we know Bernsteins basis function. But remember how I said functions, plural! Well, the next step in getting our splines working, is that we have to pick what kind of splines we want in our little demo. For starters, we'll pick quadratic Bézier splines, because they're the easiest to get going. Ok, so now we know that we want quadratic bézier splines. We have to derive some functions from our almighty basis function. So here we go. For quadratic Bézier splines, we must square each side of Bernsteins basis function. For cubic Bézier splines, we would cube each side, and so on. So Bernsteins basis, with both sides squared will look like this: 1^2 = (t + (1 - t))^2 1 = t^2 + 2*t*(1-t) + (1-t)*(1-t) You'll notice that a lot of this I didn't simplify, that's because it's easier to understand and code if you leave it this way. Ok, now we have our 3 functions, which we derived from our basis. But where you ask. Well, right in front of you! Our functions are each of the terms to the right side of the equation. We'll call our functions Bx(t). It should be noted that the code is in bold. #define B1(t) (t*t) #define B2(t) (2*t*(1-t)) #define B3(t) ((1-t)*(1-t)) You should notice here that in a quadratic bézier curve, their are 3 functions, in a cubic bézier spline, there are 4, and so on. In the zip file associated with this article, I've already derived the 4 functions for a cubic, and the 5 functions for a quartic, for you, so you don't have to do the math! Look in "Functions.txt". Ok, now that we've gotten pass the simple math behind bézier curves, lets talk about programming with them!!
|