c++ - Error in parametric equation on my opengl/glut.h code -


this question:

the parametric equation on below sets curve t ∈ r:

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)

use gl_line_strip white curve , gl_points last calculated point in red. drawn should drawing in real time, means, @ every moment, new point calculates , that, see curve being constructed, picture below. use callback timer 10 millisecond waiting time. clue: use ortho range -2.0 2.0 in x , y.

image: http://i.stack.imgur.com/s6tic.png

my code done, 2 issues.

the first 1 is: image not in curve! it's creating straight lines between calculated points. don't know if did formula wrong, if it's missing parenthesis in somewhere or that.

the second 1 is: red point being created, old 1 still there. need in order remove old ones, not big problem, first 1 crazing me out.

this code: ps: comments in portuguese first language.

#include <gl/glut.h> #include <stdlib.h> #include <math.h> void display(void); void init(void); void reshape (int w, int h);  void desenhaeixos(); void projecao(void); void desenhacurvalinha(float centerx, float centery); void timer (int i); void desenhaponto(float centerx, float centery);  float minx = -2; //parâmetros glortho float maxx = 2; float miny = -2; float maxy = 2; float minz = -1; float maxz = 1;  int k = 0;  int main(int argc, char *argv[]){     //escopo de criação de janela     glutinit(&argc, argv);//avisa que será criada uma janela     glutinitwindowsize(500,500); //diz o tamanho da janela     glutinitwindowposition(10,10); //diz onde janela vai abrir na tela, em pixel     glutinitdisplaymode(glut_rgb | glut_double | glut_depth); //prepara o sistema para janela ser criada, é extremamente importante      glutcreatewindow("freeglut shapes");//cria janela      //escopo de registro de callbacks     glutdisplayfunc(display);     glutreshapefunc(reshape);         //glutidlefunc(idle);     gluttimerfunc(1000/*valor em milesegundos*/, timer, 1);      //demais     init();     glutmainloop();//fica esperando ação usuário        return exit_success; }    void display(void){      glclear(gl_color_buffer_bit);//limpando o buffer de cor       desenhacurvalinha(1, 1);      desenhaponto(1, 1);       glutswapbuffers();      }    void init(void){       glclearcolor(0,0,0,0);//escolhe cor fundo da janela, nesse caso, preto }    void desenhaeixos(){       //gllinewidth(3); //caso queira mudar espessura da linha       glbegin(gl_lines); //indica que vou desenhar linhas            glcolor3f(1,0,0); // vermelho           glvertex3f(minx, 0, 0); //estamos escrevendo o eixo x na tela           glvertex3f(maxx, 0, 0);// fui mínimo até o máximo, da esquerda da tela até direita            glcolor3f(0,1,0); //verde           glvertex3f( 0, miny, 0);           glvertex3f( 0, maxy, 0);            glcolor3f(0,0,1); //azul           glvertex3f( 0, 0, minz);           glvertex3f( 0, 0, maxz);       glend();    }    void desenhacurvalinha(float centerx, float centery){      float x, y;      int t;      glcolor3f(1,1,1);      glbegin(gl_line_strip);           for(t=0;t<=k;t++){                 x = sin(t) + 0.5 * sin (5 * t) + 0.25 * cos (2.3*t);                y = cos(t) + 0.5 * cos (5 * t) + 0.25 * sin (2.3*t);                glvertex2f(x, y);                 }        glend(); }  void desenhaponto(float centerx, float centery){      float x, y;      int t;      glpointsize(5);      glcolor3f(1,0,0);      glbegin(gl_points);           for(t=0;t<=k;t++){                 x = sin(t) + 0.5 * sin (5 * t) + 0.25 * cos (2.3*t);                y = cos(t) + 0.5 * cos (5 * t) + 0.25 * sin (2.3*t);                glvertex2f(x, y);                 }        glend(); }    void reshape (int w, int h){      glviewport(0,0,w,h); //linha protocolo      projecao(); }    void projecao(void){       glmatrixmode(gl_projection);      glloadidentity();      glortho(minx, maxx, miny, maxy, minz, maxz); // diz agora, que o x da minha janela começa no -10 e termina no 10, o y também.      glmatrixmode(gl_modelview);      glloadidentity();  }    void timer (int i){         k++;           glutpostredisplay();       gluttimerfunc(1000, timer, 1);  } 

you straight lines between points because you're drawing straight lines between points (and that's can on pixel display).

the t in formula continuous, t ∈ r.
t isn't, , can't because you're using digital computer.

for smoother "curve", use smaller steps 1 between points on line.

you're drawing many points because you're using loop draws points on curve (i suspect copy-and-paste involved).

remove loop in desenhaponto , draw last point, t = k:

glbegin(gl_points);     x = sin(k) + 0.5 * sin (5 * k) + 0.25 * cos (2.3*k);     y = cos(k) + 0.5 * cos (5 * k) + 0.25 * sin (2.3*k);     glvertex2f(x, y);       glend(); 

Comments