简体   繁体   中英

How to implement adaptive subdivision algorithm for curve in C

My homework is to write a C program with openGL/Glut which, after getting groups of 4 points by mouse click (points with 3 coordinates), should draw a bezier curve with adaptive algorithm. At a theoretical level it's clear how the algorithm works but I don't know how to put that in C code. I mean that at lesson we saw that the 4 control points could have a shape similar to a "trapeze" and then the algorithm calculates the two "heights" and then checks if they satisfy a tollerance. The problem is that the user might click everywhere in the screen and the points might not have trapeze-like shape...so, where can I start from? This is all I have

在此处输入图片说明

This is the cole I have written, which is called each time a control point is added:

if (bezierMode == CASTELJAU_ADAPTIVE) {
    glColor3f (0.0f, 0.8f, 0.4f); /* draw adaptive casteljau curve in green */
    for(i=0; i+3<numCV; i += 3)
        adaptiveDeCasteljau3(CV, i, 0.01);
}


void adaptiveDeCasteljau3(float CV[MAX_CV][3], int position, float tolerance)  {

  float x01 = (CV[position][0] + CV[position+1][0]) / 2;
  float y01 = (CV[position][1] + CV[position+1][1]) / 2;

  float x12 = (CV[position+1][0] + CV[position+2][0]) / 2;
  float y12 = (CV[position+1][1] + CV[position+2][1]) / 2;

  float x23 = (CV[position+2][0] + CV[position+3][0]) / 2;
  float y23 = (CV[position+2][1] + CV[position+3][1]) / 2;

  float x012 = (x01 + x12) / 2;
  float y012 = (y01 + y12) / 2;

  float x123 = (x12 + x23) / 2;
  float y123 = (y12 + y23) / 2;

  float x0123 = (x012 + x123) / 2;
  float y0123 = (y012 + y123) / 2;

  float dx = CV[3][0] - CV[0][0];
  float dy = CV[3][1] - CV[0][1];

  float d2 = fabs(((CV[1][0] - CV[3][0]) * dy - (CV[1][1] - CV[3][1]) * dx));
  float d3 = fabs(((CV[2][0] - CV[3][0]) * dy - (CV[2][1] - CV[3][1]) * dx));

  if((d2 + d3)*(d2 + d3) < tolerance * (dx*dx + dy*dy)) {

      glBegin(GL_LINE_STRIP);
          glVertex2f(x0123, y0123);
      glEnd();

      return;
  }

  float tmpLEFT[4][3];
  float tmpRIGHT[4][3];

  tmpLEFT[0][0] = CV[0][0];
  tmpLEFT[0][1] = CV[0][1];
  tmpLEFT[1][0] = x01;
  tmpLEFT[1][1] = y01;
  tmpLEFT[2][0] = x012;
  tmpLEFT[2][1] = y012;
  tmpLEFT[3][0] = x0123;
  tmpLEFT[3][1] = y0123;

  tmpRIGHT[0][0] = x0123;
  tmpRIGHT[0][1] = y0123;
  tmpRIGHT[1][0] = x123;
  tmpRIGHT[1][1] = y123;
  tmpRIGHT[2][0] = x23;
  tmpRIGHT[2][1] = y23;
  tmpRIGHT[3][0] = CV[3][0];
  tmpRIGHT[3][1] = CV[3][1];

  adaptiveDeCasteljau3(tmpLEFT, 0, tolerance);
  adaptiveDeCasteljau3(tmpRIGHT, 0, tolerance);


}

and obviously nothing is drawn. Do you have any idea?

开始/结束应该吞没你的整个循环,而不是在每个孤立的顶点里面!

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