3
My question is this::
Let the parametric equations given below define a curve as a function of t R:
x(t) = sin(t) + 1/2(half a) sin(5t) + 1/4(a quarter) cos(2,3t)
y(t) = cos(t) + 1/2(half a) cos(5t) + 1/4(a quarter) sin(2,3t)
Use GL_LINE_STRIP to draw the curve line in white, and GL_POINTS to draw the last calculated point, in red color. Make sure the drawing is done in real time, that is, each instant, a new point is calculated, and in this way we will see the construction of the running curve, as illustrated in the figure below. Use the callback timer using 10 milliseconds as a standby time. Tip: use ortho ranging from -2.0 to 2.0 in x and y.
My code is practically ready, only with only two problems.
The first is: He’s not bending! He’s simply creating straight lines. I don’t know if I converted the wrong formula.
The second is: The new red dot is created, but the old one is still there. I still have to see a way to erase or overwrite the old one, but this by far is not the most important problem.
Follows my code
#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 do 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 a janela vai abrir na tela, em pixel
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //Prepara o sistema para a janela a ser criada, é extremamente importante
glutCreateWindow("FreeGLUT Shapes");//Cria a 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 ação do 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 a cor do fundo da janela, nesse caso, preto
}
void desenhaEixos(){
//glLineWidth(3); //Caso queira mudar a 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 do mínimo até o máximo, da esquerda da tela até a 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);
}