ID:18355
 
Keywords: graph, graphing, math
Well I kind of stopped working on my graphing program a while back, and I thought, what the heck. Why not let it be open source. I really doubt that it is the kind of thing that will promote thousands of rips. I hope that it is of some use/interest to some of you.

Photobucket - Video and Image Hosting

Just download Graph_src from my hosted files.
Enjoy!

Please don't hesitate to ask questions.
Whilst trying to create something similar, I've run into a problem. When I try plotting the graph of an equation, I get gaps in between the points instead of a smoothe curve:

http://members.byond.com/DeathAwaitsU/files/plot.PNG

That's supposedly the curve y = x^2 + 5.

I know how to smoothe it out when plotting something like y = x^i for which I was using this smoothing formula:

(x^i)/(fx^(i-1))

Where fx is the greatest x co-ordinate (in terms of pixels). So if we tried plotting y = x^2 from x=0 to x=5, we'd get:

0/5^(2-1) = 0
1/5^(2-1) = 1/5
2/5^(2-1) = 2/5
3/5^(2-1) = 3/5
4/5^(2-1) = 4/5
5/5^(2-1) = 1

If we range it from -512 to 512, we get a nice curve like:

http://members.byond.com/DeathAwaitsU/files/curved.PNG

But I don't get how I would smoothe out equations such as y = x^2 + 5.

Any help?
I was having the same problem, there are a couple ways to do this.

1.Figure out a formula to determine how many points are needed to be plotted for
each equation, (I have a feeling that would require finding the derivative of the equations and then when an equation has a very steep slope increasing the number of points plotted)

2.The easy way, write a proc that draws a line between two points, and just draw a line between every two points. (Make sure that if you do it this way you scale the number of points needed for each line.) This way will look choppy for some equations but it will always produce a continuous line. (Ref. the Gr_PlotLine Proc in my prog) Since you seem to be using individual pixels it shouldn't look too bad.

3.A very hard and complicated way. Have the program solve the equation for x and then graph all the x values. That way you would have all the y values and x values. This would give the best graph, but it would require you to make a symbolic manipulation program for byond.
Using method 2, is there any technique which would draw a curve instead of a line?
Well the thing is you are probably having this problem because you are graphing every possible x value but because of the resolution of the screen these x values are pretty far apart. So there really wouldn't be much of a curve between the two points. It would look like a straight line.

But, I suppose that you could take the top point and the bottom point subtract the y value of the bottom point from the y value of the top point. That would give you the vertical distance between the two points. Convert that distance into number of points needed to fill up the empty space. Then subtract the bottom x value from the top x value and then divide that number by the number of points you need. Take the number and continuously add it to the bottom numbers x value and graph each time.

So lets say you have (1,6) and (2,9) that would give you a height of 3. Take and convert it to how many points you can actually graph in a range of 3. If your resolution allows you to graph every 3rd number then you would only have to graph one point. If you graphed every tenth then you would have to graph 30 points. So let’s go with tenths (0.1). Subtract the x values with leaves you with 1. And then multiply that number by the resolution, (0.1*1=0.1). Then add that number to the x value of the bottom point and graph each time. So plug 1.1, 1.2, 1.3 and so on into the equation and graph each time. (Make sure you round to the right resolution.)