View Full Version : Displaying a math function as a graph
I've been given a function that I'm supposed to display with dots and lines:
f(x) = 300 - 100 cos(2 pi x/100) + 30 cos (4 pi x/100) + 6 cos (6 pi x/100)
I haven't taken a math course in years, so I wouldn't know where to begin to solve it, but from what I've heard, it's not necessary to know that (but I do know what it's supposed to look like). I was told that it's a simple matter as long as you know how to code it properly; however, that's where I've having problems. I'm not getting any errors with my code, for what that's worth, but it's not drawing anything at all. Any suggestions?
10-03-2005, 01:11 PM
Are you sure that your question is related to openGL in any way? :) ( if yes then you should probably show us your drawing code )
10-03-2005, 01:46 PM
Dear Ann, i'll try to give some suggestions, but i don't know if they are in line with your problem.
You say that your drawing code is ok, don't you?
1) do you use glFlush() or glFinish() after your display code?
2) are you using double buffering and you don't swap the buffers by glutSwapBuffers() after the drawing code?
3) Is the sight right pointed towards the graph?
4) do you redraw the scene without loading the identity matrix for first(by glLoadIdentity())?
5) (unsure) Has your function values so high or so low that you can't see the curve near the origin of axes(but it would be strange, because you're working with cosines, so periodical oscillations)?
I hope at least one of these issue matches your case!
Unfortunately i didn't understand if your problem is just visualizing the graph of your function or how to use openGL to build this graph. If it is the first case, the answer could be a mistake in your drawing code(i.e. a wrong use of flushing or some other features); if it is the second, i think you should give us more information about what you need.
10-03-2005, 03:59 PM
You need to set up the viewport and projection appropriately. You map be plotting off the screen or getting clipped.
Set the viewport to fill your window.
Set the matrix mode to the modelview matrix.
Load the identity matrix on the model matrix.
Call glOrtho to enclose the region you draw your graph in.
Clear the screen to black.
glColor3f(1.0, 1.0, 1.0);
/* loop through x and compute y here */
glVertex3f(x, y, 0.0); /* x y and z need to fall inside glOrtho volume */
Another non-opengl related thought:
make sure you're cast everything to a floating point number. If, for example, you have an integer loop over the number of pxiel columns that you want to raster, then you want to make sure that you've cast your for() loop var to a float while you map it to the function domain. Otherwise you'll likely end up plotting f(x) | x=0 \forall x in your domain.
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.