ackuang
April 8th, 2011, 05:33 PM
1)Why the looping in sinegraph function cannot loop for many times?
2)Why it cannot stop when i click the letter other than Y or y?
3)How to let the user to key in x3 before the glutcreatewindow to execute?
4)Anything else need to improve in this coding?
Hopefully someone can help me solve it. Thanks^^
Below is the 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);
}
}
2)Why it cannot stop when i click the letter other than Y or y?
3)How to let the user to key in x3 before the glutcreatewindow to execute?
4)Anything else need to improve in this coding?
Hopefully someone can help me solve it. Thanks^^
Below is the 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);
}
}