Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casteljau's algorithm - practical example

I have a dataset with about 50 points (x,y) and I would like to draw a smooth curve that can pass as closer as possible on those points.

I have heard about Casteljau's algorithm for splines but after hours searching on google I was not able to find a single piece of code I can use.

As far as I understood, to use this algorithm, I have to divide my dataset in groups of 4 points, right? 1234 5678 etc.. and as far as I noticed, my only problem is to find the points in the middle of each group. I mean, if I am calculating a curve for points 1234, I already have points 1 and 4 and I need to calculate 2 and 3, right? But it is a mystery to me how to do that.

I would like to ask you guys if you know some code in C, C++ or Objective-C that computes the curves based on datasets with any amount of number.

What I need is: I send the code an array with the dataset and I receive back an array with the points to draw.

My math is rusty. So, please give me practical examples. Do not send me to pages with math theory and equations. Looking at these pages makes my brain hurt...

Just tell me what to do with the points I have to compute the bezier.

Answer as you would ask a 10 year old child... :D

thanks.

like image 904
Duck Avatar asked Jun 07 '11 21:06

Duck


People also ask

Where are Bezier curves used?

Bézier curves have a lot of applications in the areas of science, engineering, and technology such as: railway route or highway modeling, networks, animation, computer-aided design system, robotics, environment design, communications, and many other fields due to their computational simplicity and stability.

Which of the following is an example of application of Bezier curves?

Bezier curves are used to outline movement in animation applications such as Adobe Flash and synfig. Users outline the wanted path in bezier curves. The application creates the needed frames for the object to move along the path. For 3D animation, bezier curves are often used to define 3D paths as well as 2D curves.

How do you calculate the control point on a Bezier curve?

Any series of 4 distinct points can be converted to a cubic Bézier curve that goes through all 4 points in order. Given the starting and ending point of some cubic Bézier curve, and the points along the curve corresponding to t = 1/3 and t = 2/3, the control points for the original Bézier curve can be recovered.

WHAT IS curve and explain Bezier curve with mathematical equations?

Bezier Curves Bezier curve is discovered by the French engineer Pierre Bézier. These curves can be generated under the control of other points. Approximate tangents by using control points are used to generate curve. The Bezier curve can be represented mathematically as − n∑k=0PiBni(t)


1 Answers

How about in C#?

private void drawCasteljau(List<point> points) {
            Point tmp;
            for (double t = 0; t <= 1; t += 0.001) { 
                tmp = getCasteljauPoint(points.Count-1, 0, t);
                image.SetPixel(tmp.X, tmp.Y, color);
            }
        }


    private Point getCasteljauPoint(int r, int i, double t) { 
        if(r == 0) return points[i];

        Point p1 = getCasteljauPoint(r - 1, i, t);
        Point p2 = getCasteljauPoint(r - 1, i + 1, t);

        return new Point((int) ((1 - t) * p1.X + t * p2.X), (int) ((1 - t) * p1.Y + t * p2.Y));
    }

From Here:

http://protein.ektf.hu/book/export/html/51

like image 161
Brad Cunningham Avatar answered Sep 27 '22 19:09

Brad Cunningham