[英]Error in parametric equation on my opengl/glut.h code
這是問題:
下面的參數方程式設置了一條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)
使用GL_LINE_STRIP繪制白色曲線,並使用GL_POINTS繪制最后一個紅色的計算點。 繪制的圖應該是實時繪制的,這意味着,每時每刻都會計算出一個新點,由此我們將看到正在構建的曲線,如下圖所示。 使用回調計時器,將等待時間設為10毫秒。 提示:在x和y中使用-2.0到2.0的正交范圍。
我的代碼差不多完成了,但是有兩個問題。
第一個是:圖像不在曲線中! 它在計算出的點之間創建直線。 我不知道該公式是否有誤,是否在某處或諸如此類的地方缺少括號。
第二個是:正在創建紅點,但是舊的點仍然存在。 我需要做一些事情以刪除舊的文件,但這不是一個大問題,第一個是使我發瘋。
這是我的代碼:PS:注釋是葡萄牙語,因為它是我的母語。
#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);
}
在點之間可以得到直線,因為您可以在點之間繪制直線(這就是在像素顯示器上可以做的所有事情)。
公式中的t
是連續的,t∈R。
你t
不是,也不可能是因為您使用的是數字式計算機。
為了使曲線更平滑,請在直線上的點之間使用小於1的步長。
您還繪制了許多點,因為您正在使用在曲線上繪制所有點的循環(我懷疑涉及復制和粘貼)。
刪除desenhaPonto
的循環,僅繪制最后一點,其中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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.