I am trying to create a fool proof graphing calculator using python and pygame. I created a graphing calculator that works for most functions. It takes a user string infix expression and converts it to postfix for easier calculations. I then loop through and pass in x values into the postfix expression to get a Y value for graphing using pygame. The first problem I ran into was when taking calculations of impossible things. (like dividing by zero, square root of -1, 0 ^ non-positive number). If something like this would happen I would output None and that pixel wouldn't be added to the list of points to be graphed. * I have showed all the different attempts I have made at this to help you understand where I cam coming from. If you would like to only see my most current code and method, jump down to where it says "current". Method 1 My first method was after I acquired all my pixel values, I would paint them using the pygame aalines function. This worked, except it wouldn't work when there were missing points in between actual points because it would just draw the line across the points. (1/x would not work but something like 0^x would) This is what 1/x looks like using the aalines method Method 1.1 My next Idea was to split the line into two lines every time a None was printed back. This worked for 1/x, but I quickly realized that it would only work if one of the passed in X values exactly landed on a Y value of None. 1/x might work, but 1/(x+0.0001) wouldn't work. Method 2 My next method was to convert the each pixel x value into the corresponding x point value in the window (for example, (0,0) on the graphing window actually would be pixel (249,249) on a 500x500 program window). I would then calculate every y value with the x values I just created. This would work for any line that doesn't have a slope > 1 or < -1. This is what 1/x would look like using this method. Current My most current method is supposed to be a advanced working version of method 2. Its kind of hard to explain. Basically I would take the x value in between each column on the display window. For every pixel I would do this just to the left and just to the right of it. I would then plug those two values into the expression to get two Y values. I would then loop through each y value on that column and check if the current value is in between both of the Y values calculated earlier. size is a list of size two that is the dimensions of the program window. xWin is a list of size two that holds the x Min and x Max of the graphing window. yWin is a list of size two that holds the y Min and y Max of the graphing window. pixelToPoint is a function that takes scalar pixel value (just x or just y) and converts it to its corresponding value on the graphing window pixels = [] for x in range(size[0]): leftX = pixelToPoint(x,size[0]+1, xWin, False) rightX = pixelToPoint(x+1, size[0]+1, xWin, False) leftY = calcPostfix(postfix, leftX) rightY = calcPostfix(postfix, rightX) for y in range(size[1]): if leftY != None and rightY != None: yPoint = pixelToPoint(y,size[1],yWin, True) if (rightY <= yPoint <= leftY) or (rightY >= yPoint >= leftY): pixels.append((x,y)) for p in pixels: screen.fill(BLACK, (p, (1, 1))) This fixed the problem in method 2 of having the pixels not connected into a continuous line. However, it wouldn't fix the problem of method 1 and when graphing 1/x, it looked exactly the same as the aalines method. ------------------------------------------------------------------------------------------------------------------------------- I am stuck and can't think of a solution. The only way I can think of fixing this is by using a whole bunch of x values. But this way seems really inefficient. Also I am trying to make my program as resizable and customizable as possible so everything must be variably driven and I am not sure what type of calculations are needed to find out how many x values are needed to be used depending on the program window size and the graph's window size. I'm not sure if I am on the right track or if there is a completely different method of doing this, but I want to create my graphing calculator to able to graph any function (just like my actual graphing calculator). Edit 1 I just tried using as many x values as there are pixels (500x500 display window calculates 250,000 y values). Its worked for every function I've tried with it, but it is really slow. It takes about 4 seconds to calculate (it fluctuates depending on the equation). I've looked around online and have found graphing calculators that are almost instantaneous in their graphing, but I cant figure out how they do it. This online graphing calcuator is extremely fast and effective. There must be some algorithm other than using a bunch of x values than can achieve what I want because that site is doing it.. Continue reading...