A traditional use of the Math Parser class is included in the tutorial entitled "A Simple Function Grapher" in the Intermediate section of the flashandmath.com website. On this final page of the Math Parser tutorial, we merely point out exactly how the Math Parser is integrated into that application. We encourage the interested reader to go through that tutorial separately to learn about the issues of graphing that are independent of the parsing expressions. Click here or on the screen shot below to open the example in a new window.
This application sets up a box for drawing the graph and helper functions for converting between screen (pixel) coordinates and (x,y) coordinates of the mathematical graph object. Within this application is the function drawGraph. We include the definition of this function below in order to focus on the role of the Math Parser.
.........
function drawGraph():void {
var pixArray:Array=[];
var i:int;
var xstep:Number;
var curx:Number;
var cury:Number;
//The next variable will store the user's input for f(x).
var sFunction:String="";
var compObj:CompiledObject;
/*
We are storing the user's input for f(x). If the input box is empty,
the function drawGraph quits.
*/
sFunction=fInputBox.text;
if(sFunction==""){
return;
}
compObj=procFun.doCompile(sFunction);
if(compObj.errorStatus==1){
ErrorBox.visible=true;
ErrorBox.text="Error in f(x). "+compObj.errorMes;
isError=true;
shAxes.graphics.clear();
return;
}
xstep=(nXmax-nXmin)/nPoints;
for(i=0;i<=nPoints;i++){
curx=nXmin+xstep*i;
/*
Calculate the consecutive value of x and obtain the corresponding value of y by
evaluating the f(x) formula for x=curx by evoking the procFun.doEval method of MathParser.
*/
cury=procFun.doEval(compObj.PolishArray,[curx]);
// Convert (x,y) coordinates to pixel values and store in pixArray for drawing later.
pixArray[i]=[xtoPix(curx),ytoPix(cury)];
}
shGraph.graphics.lineStyle(1,0xFF0000);
// Connect the dots between consecutive points that have reasonable y coordinates.
for(i=0;i<nPoints;i++){
if (isDrawable(pixArray[i][1]) && isDrawable(pixArray[i+1][1])){
shGraph.graphics.moveTo(pixArray[i][0],pixArray[i][1]);
shGraph.graphics.lineTo(pixArray[i+1][0],pixArray[i+1][1]);
}
}
}
...............
Download
- Go to the Simple Function Grapher tutorial in the Intermediate area to get all of the downloads and instructions.
Notes
In addition to this detailed tutorial on the flashandmath.com site, there are several articles posted in the MathDL Flash Forum that provide additional examples. We recommend the following:











