🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Creating smooth quadric bezier curves for any length and angle?

Started by
1 comment, last by cadjunkie 6 years, 6 months ago

Hi

I have a basic setup currently where i create a quadratic bezier curve, and then i do 100 steps across the curve to calculate its distance. And from that i do distance / 3 to give me the total number of segments.

Its pretty quick but its not a good solution for very large distances (too many segments) and then for very small distances (too few segments).

Here is a visual issue of the problem:

tRs2zQM.png

 

7 looks reasonable (though i actually think it computed 6 my mistake), but for something much shorter 2 simply doesn't cut it neither would 3 or 4 to be honest.

So i am wondering what is a common way to balance this so they are smooth regardless of length and sharpness of the angle ?

I use C# but I'm just looking for a logical approach here not how to implement it in code since i'm sure i can handle that myself :)

Thanks in advance.

Advertisement

Getting the arc length of a Bezier curve is a non-trivial problem. You can certainly estimate it via length of a polyline, but as you found out there have to be better ways. Which way you take really depends though on your desired level of accuracy, but numerical integration is the way to go here.

One interesting way you might have success with is detailed by Mike "Pomax" Kamermans here. He uses Gaussian quadrature to estimate the length of the Bezier curve. His explanation of how this works is good. I'm not sure how familiar you are with the math (Wikipedia is actually good at explaining Gauss quadrature), but I would highlight a couple of things just in case:

  • The components of a Bezier curve B(x(t), y(t)) are just Bernstein polynomials x(t) and y(t) (or if you prefer to think of them as such, 1-D Bezier curves). This is important because they just evaluate to numbers, not points or vectors.
  • You can take the derivatives of Bernstein polynomials the same way you do for Bezier curves. This will help with constructing the function that the Gauss quadrature will use.
  • For a degree-n non-rational Bezier curve, the derivative will be degree-(n-1). However, if your Bezier curve is rational (meaning the control point weights are something other than 1), then the derivative will be degree-(2n) (Section 2.7 of the material here).
  • Gaussian quadrature is exact only for polynomials of degree-(2m-1), where m is the number of Gaussian points to be used. For quadratic curves (degree-2), you can probably get away with only using 2 Gauss points. However, the function used by the Gauss quadrature is the square root of a polynomial that's degree-4. I'm an engineer and not a mathematician, but I have the feeling the resulting function can't necessarily be said to be degree-2. I personally would be safe and plan on it being a degree-4, which means that you should use 3 Gauss points at least.
  • Gauss quadrature is defined over the interval [-1,1]. Bezier curves are defined over the interval [0,1], so just remember that when reading Pomax's explanation.

I've also seen a variant of this technique using Clenshaw-Curtis integration and an approximation of the Bezier curve arc length function using Chebyshev polynomials and it works as well. Again, it depends highly on your use case and your desired accuracy.

This topic is closed to new replies.

Advertisement