- 分享
- 0
- 人气
- 0
- 主题
- 160
- 帖子
- 3215
- UID
- 168423
- 积分
- 51062
- 阅读权限
- 30
- 注册时间
- 2008-9-24
- 最后登录
- 2019-10-12
- 在线时间
- 12499 小时
|
本帖最后由 kuang 于 2011-4-12 12:56 PM 编辑
1)为什么在void sinegraph function里面的loop只可以loop几次,不会一直loop?
2)为什么我按Y或y以外的字母他还是会继续的?
3)要怎样在createwindow之前先让user输入的东西也就是x3,然后才弹出graph的window,show graph出来?
4)还有什么要改进的吗?
有哪位大大知道如何解决?小弟先感谢各位。。。以下是我的coding
#include <iostream>
#include <cmath>
#include <gl\glut.h>
using namespace std;
#define SIZE 700
#define PI 3.14159265
void menu();
void sinegraph();
void cosinegraph();
void tangentgraph();
void drawline(float x1, float y1, float x2, float y2);
void selection();
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(700,500);
glutInitWindowPosition(650,100);
menu();
selection();
glutMainLoop();
return 0;
}
void menu()
{
cout<<"******** Welcome to Trigonometric World ********"<<endl<<endl;
cout<<"1. Sine Graph"<<endl;
cout<<"2. Cosine Graph"<<endl;
cout<<"3. Tangent Graph"<<endl;
cout<<"4. Quit"<<endl<<endl;
cout<<"************************************************"<<endl<<endl;
}
void sinegraph()
{
float x1, x2, x3, y1, y2;
char again;
do
{
cout<<"Enter period(number of cycles) you want : ";
cin>>x3;
for (int i=-700;i<SIZE;i++)
{
x1=((float)i/SIZE);
x2=(((float)(i+1))/SIZE);
y1=sin(x1*x3*PI);
y2=sin(x2*x3*PI);
drawline(x1,y1,x2,y2);
}
glutSwapBuffers();
cout<<"Do you want to continue to draw? (Y/N) ";
cin>>again;
}while(again=='Y' && again=='y');
}
void cosinegraph()
{
float x1, x2, y1, y2;
for (int i=-700;i<SIZE;i++)
{
x1=((float)i/SIZE);
x2=(((float)(i+1))/SIZE);
y1=cos(x1*2*PI);
y2=cos(x2*2*PI);
drawline(x1,y1,x2,y2);
}
glutSwapBuffers();
}
void tangentgraph()
{
float x1, x2, y1, y2;
for (int i=-700;i<SIZE;i++)
{
x1=((float)i/SIZE);
x2=(((float)(i+1))/SIZE);
y1=tan(x1*2*PI);
y2=tan(x2*2*PI);
drawline(x1,y1,x2,y2);
}
glutSwapBuffers();
}
void drawline(float x1, float y1, float x2, float y2)
{
//x-axis
glBegin(GL_LINES);
glColor3f(1.0, 0, 0);
glVertex2f(-10.0f, 0.0f);
glVertex2f(10.0f, 0.0f);
glEnd();
//y-axis
glBegin(GL_LINES);
glColor3f(1.0, 0, 0);
glVertex2f(0.0f, -10.0f);
glVertex2f(0.0f, 10.0f);
glEnd();
//curve
glBegin(GL_LINE_STRIP);
glColor3f(1.0,1.0,1.0);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void selection()
{
int choice;
cout<<"Enter the type of graph you want(1,2,3 or 4) : ";
cin>>choice;
switch(choice)
{
case 1: glutCreateWindow(" Sine Graph ");
glutDisplayFunc(sinegraph);
break;
case 2: glutCreateWindow(" Cosine Graph ");
glutDisplayFunc(cosinegraph);
break;
case 3: glutCreateWindow(" Tangent Graph ");
glutDisplayFunc(tangentgraph);
break;
default:exit(0);
}
} |
|