Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Graph paper function and graphing utilities

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 23rd Dec 2010 05:11 Edited at: 23rd Dec 2010 13:06
So... I've still been doing math and graphing stuff. One thing that annoys me is that I don't have a really nice looking graph paper function for presenting stuff. Also, that last graph paper function I made wasn't that great or efficient. I haven't gone through any lengths to make this more efficient, but it definitely is cleaner, it doesn't draw over itself (well, a couple pixels, but no whole identical lines), and it doesn't draw offscreen, so I think it should be faster. It also looks better - not only due to the color scheme, but also due to a nice random textured background.

That code can be found here.
move your mouse around and click to change preferences.

That's just the plain graph paper. I've since added some handy graphing utilities. It now requires D3D_func and matrix1utils (for function pointers, and the exp() function)
As is handy in graphing stuff, there is a distinction between graph space and screen space. You can convert between the two using the functions pix_to_units and units_to_pix. Pixels, as you know, start at 0,0 (top left of the screen), and go to screen width,screenheight (bottom right). In my program, with units, negative x units is left on the screen, positive is right. Negative y units is down on the screen, positive is up.


list of things to do:

graphs that attempt to not make supah long lines at asymptotes
dashed/dotted lines
anti-aliased lines
nice functions for different kinds of graphs (polar, parametric, f(x)=y)
utilities for graphs with many outputs per input


star = implemented
light bulb = I know how ta do it
coffee = not much thought put into it yet.

The magic happens in the function:
function drawF_x(func as string, xresolution as float, tolerance as float, flags as integer, color as dword)
where "flags" is formatted as follows:


(-U means unimplemented)


anyways, here's the code. Press q/w/e/r/t to show different graphs. Going from q to t, the graphs are:
q: a parabola described in polar coordinates
w: a parabola described as f(x)=y
e: cos(x*360)=y
r: a cool polar graph (cos(x)*sin(x)*e^(cos(x))
t: the bifurcation diagram of the logistic map


The code describing those five graphs is only 47 lines long, and its easy to add more graphs or take away graphs. Just set the return values of the global "ret" (x,y for parametric or f(x)=y graphs, r for polar graph; draw=1 if it is to be drawn, 0 if not; more=1 if the graph has many values for each input and has the nonstandard flag set to 1), and call the function with the desired flags.

Anyways, heres the code/demo. Remember: it needs d3dfunc, and matrix1 function pointers and exp().



aaand a pic. The code implementing the actual line (red) is only 20 lines long, + 1 line in the loop to draw it.


Login to post a reply

Server time is: 2024-03-28 11:55:31
Your offset time is: 2024-03-28 11:55:31