# Question about graph drawing

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);

}

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 << "
";

``````switch (Orderpoly) {

case 1:
cout&lt;&lt;"Enter coefficient: a: ";
cin&gt;&gt; a ;
break;

case 2:
cout&lt;&lt;"Enter coefficients: a,b: ";
cin&gt;&gt; a &gt;&gt; b ;
break;

case 3:
cout&lt;&lt;"Enter coefficients: a,b,c: ";
cin&gt;&gt; a &gt;&gt; b &gt;&gt; c ;
break;

case 4:
cout&lt;&lt;"Enter coefficients: a,b,c,d: ";
cin&gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d ;
break;

}

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

glBegin(GL_POINTS);

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

for(j=1;j&lt;screenlength ;j++)
{
y=a*(x*x*x)+b*(x*x)+c*x+d;
if (i==screenwidth-GLdouble((y-minY)/ystep))
{cout&lt;&lt;"*"; glVertex2i(x,y);}
else if (GLdouble(x/xstep)==0)
cout&lt;&lt;"|";
else if (GLdouble(maxY/ystep)==i)
cout&lt;&lt;"-";
else
cout&lt;&lt;" ";
x=x+xstep;
}
cout&lt;&lt;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

What are your mistakes?

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.

``````
#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);

}

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 << "
";

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? I’m just another CS Student and stuck too, this last code jacksmash quoted is still not working for me.
Thanks!

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