# Some Cubic Bézier Curve Questions

1. Apr 17, 2007

### RagingPineapple

I'm using a bézier curve in a Computer Game I'm working on that has four points:

p0 (start point)
p1 (directional helper for p0)
p2 (directional helper for p3)
p3 (end point)

I nabbed the drawing formula from Wikipedia and it works fine.

I have some problems though.

I really need to obtain the length of the curve, accurate to within a pixel or two if possible. I also need to be able to take the length and convert it to t.

This is necessary because fixed jumps in t (for example, incrementing it by 0.1 every frame) does not result in jumps of a fixed distance in pixels. If we were to plot dots along the curve at every 0.1 on t, the dots would not be equally spaced.

My ultimate, evil, master plan is to obtain the length of my curve, divide the length by a certain number of steps, find t for each step, and then use t to plot the X and Y coordinates of objects which follow the curve.

That'd be groovy, but I dunno how easy it'd be. Also, I managed to understand the Wiki's Bézier explanation, but I'm not too au fait with Mathematical Notation, so some explanations may be needed, heh.

Bézier curves for dummies! Someone should so write that...

2. Apr 18, 2007

### StatusX

There are bezier curves of different orders, for example, the linear one takes two points and gives:

$$\vec r(t) = (1-t) \vec x_1 + t \vec x_2$$

the quadratic one takes three points and gives:

$$\vec r(t) = (1-t)^2 \vec x_1 + 2t(1-t) \vec x_2 + t^2 \vec x_2$$

and so on, so that your case is the cubic bezier curve.

To find the length s of any curve, you just use:

$$\frac{ds}{dt} = \left| \frac{d\vec r}{dt} \right|$$

So, for example in the linear case:

$$\frac{d\vec r}{dt} = - \vec x_2 + \vec x_1$$

so:

$$\frac{ds}{dt} = |\vec x_1 -\vec x_2|$$

and the length of the Bezer curve is the integral of this function from 0 to 1, which in this case, since the function is constant, is just:

$$s = |\vec x_1 -\vec x_2|$$

which makes sense, since the linear bezier curve is just the line segment from $\vec x_1$ to $\vec x_2$. For higher order curves, the function will be more complicated, eg, for the quadratic, you'll have to integrate something like:

$$\sqrt{ a x^2 + bx +c}$$

and for the cubic, there will be terms up to $x^4$ inside the square root. These can be done, but they're a mess. I'd suggest using mathematica or something. That or search online for somebody's who's done the work already.

Last edited: Apr 18, 2007
3. Apr 19, 2007

### RagingPineapple

So... how do I find the length of my cubic Bézier curve?

Might it be something like
$$\sqrt{p_0 x^4 + p_1 x^3 + p_2 x^2 + p_3 x }$$

I'm assuming that the a, b and c vars in your quadratic example are the positions of the three points (which would need to be four for a cubic), but what is x?

And also, if a, b and c in your quadratic suggestion are the positions of the points, how does this work with both the x AND y dimensions? How do I combine them? When I was originally drawing the Bézier Curve, I could simply copy the same formula and use it for the X and Y dimensions. But obviously, with something like length we want just one value coming out the other end.

4. Apr 19, 2007

### StatusX

You need to use calculus, which it seems like you're not that familiar with. Just try searching online for someone who's done it already. If you really can't find anything, maybe me or someone else will work it our for you, but it'll be pretty tedious and probably take a little time.

5. Apr 19, 2007

### RagingPineapple

Yeah, I'm not familiar with Calculus, lol. :tongue:

I've googled and Wiki'd it, but all I can find are ways of aproximating it, and even then they're ill-explained or have too great a margin for error.

But at the same time, I don't want you to spend a long time on it if that's what it would take. I made this post rather assuming it would be simpler than that.

Thanks for your help all the same. I'm considering making a system to position dots along the curve, measure the straight lines between them, sum 'em up, and use that.

6. Apr 19, 2007