简体   繁体   中英

How do I specify the specific the control points in a De Casteljau algorithm, if I am using nested for loops to iterate?

I know using De Casteljau's algorithm is not the best way to draw a Bezier curve, but I need to implement it for an assignment I am defining my algorithm based on the following equations (from Drexel).

Where:

defines the control points.

I am trying to define the function to do the algorithm, but am struggling with where/how to incorporate the control points. The control points are defined by the user; as they interact with the program, a left click adds a new control point. My function currently looks as follows:

2Dpoint deCast(float t)
{
    2Dpoint tempDC   // Temporary value of point passed back to OpenGL draw function
    tempDC.x = 0; tempDC.y = 0    // Initialize temporary value

    int r,i;
    int n = C->B.size();   // C is pointer to B vector, which is where the control points are stored in a 2D vector

    for (r = 1; r<n, r++)
    {
        for (i = 0; i<n-r; i++)
        {
        // Calculation of deCast points goes here
        } 
    }
}

Where 2Dpoint is just a structure defined by a header file, C is a pointer to the location of the control points, which are stored in a 2Dpoint struct called B (ie the i value of the control point vector is accessed by C -> B[i].x and C -> B[i].y ). t is provided to the function when it is implemented in my draw function, as shown below.

void draw()
{
    glColor3f(0.0f, 1.0f, 0.0f);
    glLineWidth(2.0f);
    glBegin(GL_LINE_STRIP);
    float DCiter = 0;
    while (DCiter <= 1.0)
    {
        2Dpoint DC = decast(DCiter);
        glVertex2f(DC.x, DC.y);
        DCiter = DCiter + 0.01;
    }
}

You're going to have to pass the deCasteljau function your points, too, because that's the whole reason de Casteljau's algorithm works (which you even show in your mathematical formula: it reads "the next point = the weighted sum of two previous points"). Since you need to do linear interpolation between points, you need to work with points =)

In pseudo code:

deCastestljau (t, points):
  // the following code is destructive, so remember to be sensible
  points = shallowCopy(points);
  // de Casteljau's algorithm in four lines:
  while points.length > 1:
    for i=0 to points.length-2: 
      points[i] = (1-t) * points[i] + (t) * points[i+1]
    points.pop()
  // and we're done.
  return points[0]

The end: you start with your control points, then you do a linear interpolation pass so you get all the points "between successive coordinates" at distance ratio t (eg if t is 0.25, then you're finding each linear interpolation at 1/4th the distance from some point to the next).

That operation yields a new set of points 1 fewer than before. Then you do it again, and again, and again, until you're left with one point. That's your on curve point. This works for any order Bezier curve.

It also has pretty much nothing to do with "drawing" a curve, though, de Casteljau's algorithm is a way to efficiently compute single points on a curve, it makes no claims about, nor has any true relation to, drawing entire curves. That's a completely different task, for which de Casteljau's algorithm is just one of many options to find whatever on-curve points you do need to make your draw algorithm work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM