Graph an Equation
-
Hey everyone, I'm looking to allow a user to input a mathematical function (like x^2 + y^2 = 4) and graph it on the screen. I've been thinking about how to go about this, and I'm not sure if there is an easier way. Currently, I'm thinking about forcing the user to input the function in the form of y=something and then looping through all of the x-coordinates, solving the right hand side each time for each x-value and then pairing the x and y coordinates together, and drawing lines between each pair. However, this is very clunky, and doesn't allow for three-dimensional equations (when I get there). Is there some way of parsing through the equation and solving it? I know it can be done, because I've seen math programs out there that can graph equations no matter how you input them (including 3D ones). If there is somewhere I can go to learn more about this process, I'd love to hear about it, and if anyone knows of some example code, that would also be appreciated. Thanks!
-
Hey everyone, I'm looking to allow a user to input a mathematical function (like x^2 + y^2 = 4) and graph it on the screen. I've been thinking about how to go about this, and I'm not sure if there is an easier way. Currently, I'm thinking about forcing the user to input the function in the form of y=something and then looping through all of the x-coordinates, solving the right hand side each time for each x-value and then pairing the x and y coordinates together, and drawing lines between each pair. However, this is very clunky, and doesn't allow for three-dimensional equations (when I get there). Is there some way of parsing through the equation and solving it? I know it can be done, because I've seen math programs out there that can graph equations no matter how you input them (including 3D ones). If there is somewhere I can go to learn more about this process, I'd love to hear about it, and if anyone knows of some example code, that would also be appreciated. Thanks!
I can think of 2 ways to do what you want and I've used both methods, although there may be other ways. 1) Write a parser and an evaluation "engine" class. The last time I did that I based it on the Forth system so it's real fast, but I converted the system to regular algebraic rather than reverse polish notation. I'd post a link, but the last time I did that I wrote it in Pascal, not C#, and I don't have a link to any code handy. 2) Use the CodeDomProvider class to write the function in the .NET language of your choice. For users I generally stick with VB since users seem most familiar with that. I've also used JScript, C#, F# in addition to VB. My article ScriptEngine - User Defined Calculations in C#, VB, JScript and F#[^] provides an example of how to do this, but you will need to modify for your own purposes. Note, that as mentioned in some of the comments, there doesn't appear to be a good way to ever unload the code if you have a long running application. In either case you can allow more than just a simple formula, depending on how you limit things. You'll still need to evaluate the function for all your input values and handle the graphics. Perhaps someone else will have additional ideas. Good luck!
CQ de W5ALT
Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software