PDA

View Full Version : Question about graph drawing



notme_GL
12-08-2007, 07:53 AM
Hello everyone,

I am a cs-student and beginner in openGL.

During a graphics course i have the following project:

Write a C++ program in openGL to display the graph of the polynomial: P(x)= a0+a1X+a2x^2+...+anx^n, where a0,a1,a2,a3 are the coefficients of the polynomial and will be given by the user. Initially the program shoul prompt for the order (n) of the polynomial. Also the user will be asked to insert the bottom-left corner (minX, minY) and the top-right(maxX. maxY) of the display area on the graph. the xy-axis should also be displayed.

Here is how my code looks like up to now:

#include<iostream.h>
#include <windows.h> // Header File for Windows
#include <gl\gl.h> // Header File for the OpenGL32 Library
#include <gl\glu.h> // Header File for the GLu32 Library
#include <gl\glut.h> // Header File for the Glut32 Library
#include <math.h>

void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background colour
glColor3f(0.0f,0.0f,0.0f); // set the drawing colour
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

}

void myDisplay (void){
GLint screenlength=79;
GLint screenwidth=24 ,Orderpoly ;
GLfloat a,b,c,d, xstep,ystep;
cout<<"Enter the order of the polynomial (From 1 - 4): ";
cin >> Orderpoly ;
cout << "\n";

switch (Orderpoly) {

case 1:
cout<<"Enter coefficient: a: ";
cin>> a ;
break;

case 2:
cout<<"Enter coefficients: a,b: ";
cin>> a >> b ;
break;

case 3:
cout<<"Enter coefficients: a,b,c: ";
cin>> a >> b >> c ;
break;

case 4:
cout<<"Enter coefficients: a,b,c,d: ";
cin>> a >> b >> c >> d ;
break;

}


GLint i,j;
GLfloat x,y;
GLfloat minX,minY,maxX,maxY;
cout<<endl;
cout<<"Enter minX,minY:";
cin>>minX>>minY;
cout<<endl;
cout<<"Enter maxX,maxY:";
cin>>maxX>>maxY;
gluOrtho2D(minX,maxX,minY,maxY);
xstep=(maxX-minX)/(screenlength);
ystep=(maxY-minY)/(screenwidth);

glBegin(GL_POINTS);

for (i=1;i<=screenwidth;i++)
{
x= minX;

for(j=1;j<screenlength ;j++)
{
y=a*(x*x*x)+b*(x*x)+c*x+d;
if (i==screenwidth-GLdouble((y-minY)/ystep))
{cout<<"*"; glVertex2i(x,y);}
else if (GLdouble(x/xstep)==0)
cout<<"|";
else if (GLdouble(maxY/ystep)==i)
cout<<"-";
else
cout<<" ";
x=x+xstep;
}
cout<<endl;
glEnd();
glFlush(); }}

GLdouble f(GLdouble x)
{
if (x>=0)
return (GLint) (x + 0.5);
else
return (GLint) (x -0.5);
}

void main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(500,500);
glutCreateWindow("Mathematical function");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}


void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x,y); // draw a dot at point (x,y)
glEnd();
}

Any help to correct my mistakes will be much appreciated.

Thanks

jacksmash
12-08-2007, 09:56 AM
What are your mistakes?

notme_GL
12-08-2007, 10:07 AM
If you try and compile it in VC6 you will see that it doesn't give you the right graph. i think it has something to do with thw coordinates and moreover it does'n dispaly the graph inside the window i created with openGL.

jacksmash
12-08-2007, 11:28 AM
#include<iostream.h>
#include <windows.h> // Header File for Windows
#include <gl\gl.h> // Header File for the OpenGL32 Library
#include <gl\glu.h> // Header File for the GLu32 Library
#include <gl\glut.h> // Header File for the Glut32 Library
#include <math.h>

void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background colour
glColor3f(0.0f,0.0f,0.0f); // set the drawing colour
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

}

void myDisplay (void){
GLint screenlength=79;
GLint screenwidth=24 ,Orderpoly ;
GLfloat a,b,c,d, xstep,ystep;
cout<<"Enter the order of the polynomial (From 1 - 4): ";
cin >> Orderpoly ;
cout << "\n";

switch (Orderpoly) {

case 1:
cout<<"Enter coefficient: a: ";
cin>> a ;
break;

case 2:
cout<<"Enter coefficients: a,b: ";
cin>> a >> b ;
break;

case 3:
cout<<"Enter coefficients: a,b,c: ";
cin>> a >> b >> c ;
break;

case 4:
cout<<"Enter coefficients: a,b,c,d: ";
cin>> a >> b >> c >> d ;
break;

}


GLint i,j;
GLfloat x,y;
GLfloat minX,minY,maxX,maxY;
cout<<endl;
cout<<"Enter minX,minY:";
cin>>minX>>minY;
cout<<endl;
cout<<"Enter maxX,maxY:";
cin>>maxX>>maxY;
gluOrtho2D(minX,maxX,minY,maxY);
xstep=(maxX-minX)/(screenlength);
ystep=(maxY-minY)/(screenwidth);

glBegin(GL_POINTS);

for (i=1;i<=screenwidth;i++)
{
x= minX;

for(j=1;j<screenlength ;j++)
{
y=a*(x*x*x)+b*(x*x)+c*x+d;
if (i==screenwidth-GLdouble((y-minY)/ystep))
{cout<<"*"; glVertex2i(x,y);}
else if (GLdouble(x/xstep)==0)
cout<<"|";
else if (GLdouble(maxY/ystep)==i)
cout<<"-";
else
cout<<" ";
x=x+xstep;
}
cout<<endl;
glEnd();
glFlush(); }}

GLdouble f(GLdouble x)
{
if (x>=0)
return (GLint) (x + 0.5);
else
return (GLint) (x -0.5);
}

void main(int argc, char **argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(500,500);
glutCreateWindow("Mathematical function");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}


void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x,y); // draw a dot at point (x,y)
glEnd();
}

OpenGL-New
12-10-2007, 12:13 PM
Any help? I'm just another CS Student and stuck too, this last code jacksmash quoted is still not working for me.
Thanks!

ZbuffeR
12-10-2007, 01:55 PM
Define "not working".
At least show you have some problem-solving skills before asking people to do your homework.
http://www.catb.org/~esr/faqs/smart-questions.html