-4

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

enter image description here

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?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • start from providing a more comprehensive description of your problem. Do you have a pseudocode? if not how was the algorithm presented to you? – UmNyobe Sep 30 '15 at 14:04
  • I have a code but I'm afraid it's useless because up to now it draws bezier curve with the default method or with decasteljau algorithm... the adaptive algorithm was presented to me in a very general way. The teacher showed us a slide with 4 control points making a sort of trapeze inside which there is the curve. Then the two "heights" are calculated and if they don't satisfy the tolerance the middle point of each side of the trapeze is splitted into two halves and the process starts again for both the "trapezes" thus formed... – SagittariusA Sep 30 '15 at 14:09
  • Is one of the coordinates (z?) of the points always 0? Is this a 2-d or 3-d curve? – Jiminion Sep 30 '15 at 14:17
  • yes, z coordinates are always 0 and it's a 2d curve! – SagittariusA Sep 30 '15 at 14:20
  • @Jiminion : I've just added a sort of code I have seen in the web – SagittariusA Sep 30 '15 at 16:02
  • @UmNyobe : I've just added a sort of code I have seen in the web – SagittariusA Sep 30 '15 at 16:02

1 Answers1

1

the Begin / End should engulf your whole loop, not being inside for each isolated vertex !

Fabrice NEYRET
  • 644
  • 4
  • 16