Add your own alternative version
Stats
263.4K views 12.2K downloads 96 bookmarked
Posted
14 Apr 2008

Comments and Discussions


I intend to use your BezierCurve class to adjust the volume levels coming out of my electronic piano keyboard. I recently bought a nice keyboard but some notes are louder or softer than the rest. I'm going to write an application that applies custom velocity curves to the offending notes.
Thanks
Todd Piltingsrud





Todd,
This is an exciting application, let me know if you got any results.
Bests,





many thanks for the detailed description and the useful advice





why only 32 points? i don't understand. I have a list of point and I have an error in factorial can You help me?





Because:
I have a very simple factorial computation routine and 32 is already as much as I can compute as an int value. Since this code is only a demonstration, I have not bothered to go beyond that. Normally, there are smarter ways.
Cheers,





First of all this is great work!
The limitation of 32 can be easily removed. The issue is in the calculation of Bernstein basis, you used the formula with all the big numbers. If you canculate the basis succsseively, say
B(i+1) = B(i)*t*(ni)/[(1t)*(i+1)]
Then you can bypass all the factorial parts (remove the table, Ni).
Chengyou





Thank you Chengyou,
This is a design choice to speed up the calculations. As you mentioned there are many ways to get rid of such limitation. I think the best way would be to compute those coefficients by a table lookup to 32 and then compute the rest with a recursive formula, like the on you put.






Good work! I just wrote an article on codeproject and used your work. Of course I referenced to you (in code and in the article). You can find it here: Bezier curve for the Microsoft Web Chart control[^]
____________________________________________________________________
Never underestimate the power of stupid people in large groups.





Thanks Rainer, it's great to see such good use of it. Nice idea.
Tolga





Hi,
I found an interesting easy solution to solve Beziercurves: http://www.cubic.org/docs/bezier.htm[^]
Regards, Rainer
____________________________________________________________________
Never underestimate the power of stupid people in large groups.





Makalenizi çok beğendim.
Saygılarımla,
Erol Esen
Eroldynamic





Tesekkur ederim.
Saygılarımla,
Tolga





i'm trying to use the algorithm to generate curvy paths that i will move characters on these paths, but the problem is that this algorithm don't give a smooth curve, how can i fix that ?





Well, to my opinion, it clearly generates smooth curves... It doesn't even pass through the control points, like catmull rom splines do.
How smooth do you want?
Also, one quick idea that I can think of is to reduce the amount of control points. The less control points you have, the smoother the curve will look like.





This is driving me nuts  I can't figure out how to draw filled antialiased Béziers in C#/GDI+.
GDI+ offers nonfilled Béziers, and also offers other kinds of filled curves, but not filled Béziers. And I think I can forget about antialiasing.
So I'll probably have to roll my own Bézier drawing component. Google isn't helping me here. Anyone have any suggestions to offer?
Nice article btw
c





I am faced with the same problem.
The solution ought to be to create the boundary with nonfilled Beziers, choose any point inside the boundary and use FloodFill. I find this sometimes fails because the Bezier functions in my (admittedly old) Microsoft GDI library sometimes leak. That is, they sometimes create lines with a gap through which FloodFill leaks to the exterior.
To solve this I have written my own Bezier function which I am sure doesn't leak. I have experimented with different versions. My fastest version uses fixed point arithmetic and a buffer to hold all the points and then Polyline to draw them. It is only slightly (less than 20%) slower than the library version. Using LineTo for each point to avoid the buffer is many times slower. Using floating point instead of fixed point doubles the time but avoids a possible arithmetic overflow problem with too many segments. This limits my simplest version with a 1024 bit screen to 64 segments and a slightly more complex version to 1024 segments. Many suggest that 32 segments is enough anyway. Incidentally I set a maximum number of segments but unlike other code I have seen I use a recursive binary chop to save time by avoiding calculating and drawing zero length segments in small curves and avoiding calculating and drawing successive segments in a straight line in large curves.





Hello Burisch,
We are now some 6 month further after your cry for help.
If you still need some hints:
I recently needed a Bézier that should exactly fit into a gap in an existing multipoint graph.
Birdal's code, although very good and helpfull, didn't do that job and always left a hole at the end of the Bézier where it should, but didn't, connect to the other end of the gap in the existing graph.
The solution was found in a small change in the code:
change:
// step = (double)1.0 / (cpts  1);
into:
step = (double)1.0 / cpts;
Just the problem of trees and inbetween's.
Till sofar the simple approach.
A far more sophisticated solution to your antialiasing problem you might find in:
"Interpolation with Bezier Curves. A very simple method of smoothing polygons"
which is to be found in http://www.antigrain.com/agg2.5.zip
Good luck,
jvd





Very useful; well explained; nicely simplified.






Thanks for a great article!
Sorry but could you possibly supply the "Bernstein polynomials" function you are using as well? I've looked around for a way to do this but my math is quite rusty and I can't get it to work without it. If you don't have it, maybe a simple description of what I should code to get the same effect? (without the formula as that is all greek to me)
Thanks!





He can't because he's not really using them. He's faked the article and ripped off some code from gamedev.





Please look into the comments below for my answers to fresi.
Isn't Bernstein basis is in the code? It is in a separate function. Please download and check.





Big thanks for your article. Good roundup and not too long, considering the depth of the subject. I'd recommend making the FactorialLookupTable static so that every instance of BezierCurve shares the same table.
Regards,
André





Thank you André,
You are right about static look up table. I should update it. I will post it with the next update.
Tolga





I modified the code for my needs, to make it more easy to use when handling with Point structs:
public void Bezier2D(Point[] bezierPoints, int numOfPointsToCalculate, out List<Point> calculatedPoints) {
calculatedPoints = new List<Point>();
double t = 0;
double step = 1.0 / (numOfPointsToCalculate  1);
int numOfKnots = bezierPoints.Length  1;
for (int i1 = 0; i1 != numOfPointsToCalculate; i1++) {
if ((1.0  t) < 5e6) {
t = 1.0;
}
Point newPoint = new Point(0.0, 0.0);
for (int i = 0; i < bezierPoints.Length; i++) {
double basis = CalculateBernstein(numOfKnots, i, t);
newPoint.X += basis * bezierPoints[i].X;
newPoint.Y += basis * bezierPoints[i].Y;
}
calculatedPoints.Add(newPoint);
t += step;
}
}
Regards,
André





Thanks Andre,
I get happy when people work on such codes, modify it and post it back.
One key note is: I try not to allocate pointers inside functions. You either allocate the whole structure inside or allocate it outside and pass it as an argument. It is good in terms of convention (Saying as a c++ programmer).
Tolga





Hi Tolga,
thanks for your reply. I think you got a good point there and in fact I wasn't really happy with the initialization inside the Beziermethod. Thanks.
Regards,
André





thanks to the author!
here is my version, adopted for my own needs:
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Drawing;<br />
<br />
namespace Curves<br />
{<br />
public static class BezierCurve<br />
{<br />
private static double[] FactorialLookup = new double[]<br />
{<br />
1.0,<br />
1.0,<br />
2.0,<br />
6.0,<br />
24.0,<br />
120.0,<br />
720.0,<br />
5040.0,<br />
40320.0,<br />
362880.0,<br />
3628800.0,<br />
39916800.0,<br />
479001600.0,<br />
6227020800.0,<br />
87178291200.0,<br />
1307674368000.0,<br />
20922789888000.0,<br />
355687428096000.0,<br />
6402373705728000.0,<br />
121645100408832000.0,<br />
2432902008176640000.0,<br />
51090942171709440000.0,<br />
1124000727777607680000.0,<br />
25852016738884976640000.0,<br />
620448401733239439360000.0,<br />
15511210043330985984000000.0,<br />
403291461126605635584000000.0,<br />
10888869450418352160768000000.0,<br />
304888344611713860501504000000.0,<br />
8841761993739701954543616000000.0,<br />
265252859812191058636308480000000.0,<br />
8222838654177922817725562880000000.0,<br />
263130836933693530167218012160000000.0<br />
};<br />
<br />
<br />
// just check if n is appropriate, then return the result<br />
//private static double factorial(int n)<br />
//{<br />
// if (n < 0) { throw new Exception("n is less than 0"); }<br />
// if (n > 32) { throw new Exception("n is greater than 32"); }<br />
// return FactorialLookup[n]; /* returns the value n! as a SUMORealing point number */<br />
//}<br />
<br />
private static double Ni(int n, int i)<br />
{<br />
double ni;<br />
double a1 = FactorialLookup[n];<br />
double a2 = FactorialLookup[i];<br />
double a3 = FactorialLookup[n  i];<br />
//double a1 = factorial(n);<br />
//double a2 = factorial(i);<br />
//double a3 = factorial(n  i);<br />
ni = a1 / (a2 * a3);<br />
return ni;<br />
}<br />
<br />
// Calculate Bernstein basis<br />
private static double Bernstein(int n, int i, double t)<br />
{<br />
double basis;<br />
double ti; /* t^i */<br />
double tni; /* (1  t)^i */<br />
<br />
/* Prevent problems with pow */<br />
<br />
if (t == 0.0 && i == 0)<br />
ti = 1.0;<br />
else<br />
ti = Math.Pow(t, i);<br />
<br />
if (n == i && t == 1.0)<br />
tni = 1.0;<br />
else<br />
tni = Math.Pow((1  t), (n  i));<br />
<br />
//Bernstein basis<br />
basis = Ni(n, i) * ti * tni;<br />
return basis;<br />
}<br />
<br />
public static List<PointF> Bezier2D(List<PointF> bezierPoints, int numOfPointsToCalculate)<br />
{<br />
List<PointF> calculatedPoints = new List<PointF>();<br />
double t = 0;<br />
double step = 1.0 / (numOfPointsToCalculate  1);<br />
int numOfKnots = bezierPoints.Count  1;<br />
<br />
for (int i1 = 0; i1 != numOfPointsToCalculate; i1++)<br />
{<br />
if ((1.0  t) < 5e6)<br />
{<br />
t = 1.0;<br />
}<br />
<br />
double x = 0.0;<br />
double y = 0.0;<br />
int i = 0;<br />
foreach (PointF bezPoint in bezierPoints)<br />
{<br />
double basis = Bernstein(numOfKnots, i, t);<br />
x += basis * (double)bezPoint.X;<br />
y += basis * (double)bezPoint.Y;<br />
i++;<br />
}<br />
calculatedPoints.Add(new PointF((float)x, (float)y));<br />
<br />
t += step;<br />
}<br />
return calculatedPoints;<br />
}<br />
}<br />
}<br />





You clearly have a large gap in your understanding of splines. Proof of point, you describe the use of Bernstein polynomials, yet your digram is clearly explaining the De casteljau algorithm.





From Wikipedia:
In the mathematical subfield of numerical analysis the de Casteljau's algorithm, named after its inventor Paul de Casteljau, is a recursive method to evaluate polynomials in Bernstein form or Bézier curves. The de Casteljau's algorithm can also be used to split a single Bézier curve into two Bézier curves at an arbitrary parameter value.
So obviously the algorithm is used to construct Bezier Curves. Yet, if you read carefully, in my article I have two quotes:
"What this equation tells us is nothing but the formulation of the above algorithm (the midpoint iterations). It is very important in the sense that a whole algorithm could be summarized into a formula and a straightforward implementation would yield correct results."
"The objective here is to find points in the middle of 2 nearby points and iterate this until we have no more iteration. The new values of points will give us the curve. The famous Bezier equation is the exact formulation of this idea."
The algorithm may be called De Casteljau's algorithm but it is also a way of describing Bezier Curves.
Tolga





ok then seeing as though you think you understand it, using your method in your article code and all interpolate t between 01 determine the points then draw lines between said point as you would if you were to draw a spline.
If you really think your description and the image are the same thing then you should see a totally uniform distance between each line segment if your delta (or step as you call it) between t_i and t_i+1 is constant, lets say it is 0.001 and the control points will be (100,0)(50,100)(500,100)(100,0), come on show me using your code that the distance between consecutive points is equal, come on mr wikiman show me I am wrong and that you and wikipedia are correct...





Hi,
As I understand you are certainly not convinced by my statement: Berstein Polynomials is the mathematical formulation of Casteljau algorithm. I won't put the detailed proof here, but you can find any proof you want on De Casteljau algorithm here:
http://timotheegroleau.com/Flash/articles/cubic_bezier/casteljau_proof.pdf[^]
Additionally, here you will see some geometric interpretations:
http://cec.wustl.edu/~cse452/lectures/lect18_Approximation.pdf[^]
If you are not satisfied with these, please read the book:
Geometric Modeling with Splines  An Introduction
Gershon Elber, Elaine Cohen, and Richard F. Riesenfeld
For even more info you can also check this:
http://www.springerlink.com/content/b765t53217738103/fulltext.pdf[^]
As you see, it is not only me and Wikipedia telling this. These resources would give you a better proof than I do.
PS: Even though De Casteljau's algorithm is exactly stating what Bernstein Polynomials do, in many cases it is not implemented directly. The reason for this is that because of its recursive nature it is slower than implementing the formula directly. Additionally, in the Bernstein formulation you can do many precomputations which can improve performance.
Tolga





Instead of babbling on why didn't you do as I asked? was it really that hard? so you couldn't do it or you did it and found you were wrong all along. now in my my opinion the best thing you can do is to either way cleanup the article or delete it.





First of all, I won't respond to any further question looking comments of you, because they are nonsense. This is the last one I am answering.
Fresric Bouemont wrote: Instead of babbling on why didn't you do as I asked? was it really that hard? so you couldn't do it or you did it and found you were wrong all along
1) Why didn't you read the proofs? Or you read and didn't understand? I think they are straightforward and totally mathematical. They answer your question exactly. If you want to prove it is wrong, do it and show me.
2) I don't have time to write code for every dummy question that is asked around.
3) How can you comment so unconsciously on a proven mathematical fact? Don't you know any math?
4) You don't have any articles written. Take your time and spend it to write a few articles if you would like to prove me wrong that much.
Fresric Bouemont wrote: now in my my opinion the best thing you can do is to either way cleanup the article or delete it.
5) There are people who will use this article and code for learning and other purposes. Why would I remove it? Just because a timewaster wanted me to do it?
Tolga





So what you're saying is that you can't reproduce the fancy graphics you have plagiarized in your article.





One reason, I am also not responding is that I have read your other comments. Please don't do what you are trying to do and respect people's work. You are using codes from articles written here...
Tolga





Wow what an a**hole fresric bouement is! sleeze bag get off the boards please! Nice artice Tolga






General News Suggestion Question Bug Answer Joke Praise Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

