Skip to content

Commit

Permalink
support for r=f(theta) type functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony F committed Jun 18, 2011
1 parent bbaa03e commit 17f470d
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions main.js
Expand Up @@ -49,7 +49,8 @@ function compile(n){
var yfuncs=[];

//Functions of theta

var tfuncs=[];

//Functions of r
var rfuncs=[];

Expand Down Expand Up @@ -78,7 +79,9 @@ function compile(n){
yfuncs.push(eq[1]);
}else if(dirty(eq[0])=="theta"){
rfuncs.push(eq[1]);
}else if(eq[0]!=""){
}else if(dirty(eq[0])=="r"){
tfuncs.push(eq[1]);
}else if(eq[0]!=""){
//a *variable* is defined by this graph. Add it to vars.
var varname=dirty(eq[0]);
vars[varname]=eq[1].eval();
Expand Down Expand Up @@ -361,7 +364,7 @@ function compile(n){
}

//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var r=0;ctx.move(r,"+jsc+");";
builder+="ctx.beginPath();var r=0;ctx.move(0,0);";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.
Expand Down Expand Up @@ -389,6 +392,62 @@ function compile(n){
}

}

if(tfuncs.length){
for(var i=0;i<tfuncs.length;i++){

var fn=tfuncs[i].simplify();

//Get javascript expression.
var jsc=fn.getString(0,true);

//A little messy:
jsc=jsc.replace(/app\.variables\[\"(x|y|z|r|theta)\"\]/g,"$1");

if(first){
//The first value of a multi-valued function shall be its
//primary value.

ret.f=new Function("theta","return "+jsc);
first=false;
}


var singularities=[];
try{
var singularities=fn.singularities();

//We don't care about singularites way out there:
singularities.remove(Infinity);
singularities.remove(-Infinity);
}catch(ex){
if(__debug(1,0)){
//when debugging we want to see the error
throw(ex);
}
}

//begin the path. Colour (fill and stroke) is already done for us in ui.js
builder+="ctx.beginPath();var theta=0;ctx.move("+jsc+"*cos(theta),"+jsc+"*sin(theta));";
if(0 && singularities.length){
//Better plot logic.
//For example, this would allow us to plot y=sqrt(1-x^2) from [-1,1] exactly with no abrupt endings.

}else{
//The loop is within the function because calling a function is too slow in javascript (last time I checked)
//Plot each point: (jsc is the expression generated by CAS)
/*
tinc: reciprcal of pixels along max *circle* (ellipse to complicated) around screen area?
*/
builder+="var tinc=1/max(width,height);for(var theta=0;theta<(2*pi);theta+=tinc){var rtmp=("+jsc+");ctx.line(rtmp*cos(theta),rtmp*sin(theta));}ctx.line("+jsc+",0);ctx.stroke();";
}
console.log(builder);

}

}

//Add our vars and funcdefs to the global CAS variables (app.variables)
//TODO: should not be app, but cas.variables. (javascript-cas will support variables natively someday)
if(window && window.app && window.app.variables){
Expand Down

0 comments on commit 17f470d

Please sign in to comment.