-1

I'd like to know how can I use these functions to generate curves in OpenGL:

x(t) = sin(t) + 1/2 sin(5t) + 1/4 cos(2,3t)

y(t) = cos(t) + 1/2 cos(5t) + 1/4 sin(2,3t)

enter image description here

It's a college exercise, I've done something with circles, but with curves I'm having problems.

I didn't catch the way for building curves, I've tried to build a code to generate a simple curve to see how It works, but It's completly wrong because It generated something very strange. I'd like to see a simple example of a curve with this function to understanding how I must start to construct the draw like the prints.

Test code:

void exerciseTwo(){
 write("Exercise Two", -5, 18);
 
 float x = 0, y = 0, t =0;
 
 glPushMatrix();
     glBegin(GL_LINE_STRIP);
     for(t = -10.0; t < 10.0; t += 0.1){
           x = sin(t) + ((1/2) * sin(5 * t)) + ((1/4) * cos(2.3 * t));
          glVertex2f(x, t);
     }       
     
     for(t = -10.0; t < 10.0; t += 0.1){
           y = cos(t) + ((1/2) * cos(5 * t)) + ((1/4) * sin(2.3 * t));
           glVertex2f(t, y);
     }
     
     glEnd();
 glPopMatrix();
 
 write("Press 0 (zero) to come back.", -10, -18);
}

Am I in the right way or not?

Community
  • 1
  • 1
João Pedro
  • 429
  • 8
  • 20
  • have you tried using a scientific calculator and see what the curves you get from the functions look like? is as simple as making a loop to change the pixel color of each pixel in (X,T) coordinates. – ScarletMerlin Apr 21 '15 at 14:39
  • You can also plot it in a spreadsheet such as OpenOffice Calc or Microsoft Excel. Also, try going from -10*pi to 10*pi instead of -10 to 10. You can pick the increment size as you see fit but that will cover the entire range of the behavior. – andand Apr 21 '15 at 19:26

1 Answers1

2

Edited Answer : You are supposed to represent x and y part of curve as 1 vertice otherwise you get unrelated results. Here is correction :

glPushMatrix();
     glBegin(GL_LINE_STRIP);
     for(t = -10.0; t < 10.0; t += 0.1){
           x = sin(t) + ((1/2) * sin(5 * t)) + ((1/4) * cos(2.3 * t));
           y = cos(t) + ((1/2) * cos(5 * t)) + ((1/4) * sin(2.3 * t));
           glVertex2f(x, y);
     }         
     glEnd();
 glPopMatrix();

Note: sine an cosine functions are take parameter as radian type not degree. If the result still not what you want, consider this warning.

Berke Cagkan Toptas
  • 1,034
  • 3
  • 21
  • 31
  • Can you see the "Test Code" of my question? I updated the code following your tips, but the code isn't drawing something like the photos I posted. I tried to put the codes in only one for (as you told me), in two (Like I posted now), but It's generating something very different. Thank You for your answer. – João Pedro Apr 21 '15 at 16:18
  • I tried this way you posted and It generated a circle, so I converted the degree to radius ( (angleDegrees * M_PI / 180.0) ) and It still went wrong =;/ I'll need to ask my teacher how to do this... Thank you so much for your help. – João Pedro Apr 21 '15 at 18:26