function Formula(){
this.a = 0;this.b = 0;this.c = 0;
this.toString=function(){
return this.a +","+this.b+","+this.c;
};
this.getY=function(x){
return this.a*Math.pow(x,2)+this.b*x+this.c;
};
this.getX=function(y,xRange){//x=[-b±√(b^2+4a(y-c))]/(2a)
var sqrt_b2_4ac = Math.sqrt(Math.pow(this.b,2)+4*this.a*(y-this.c));
var ret = (sqrt_b2_4ac - this.b)/(2*this.a);
if (xRange[0] <= ret && ret <= xRange[1]){
return ret;
}else {
return (-sqrt_b2_4ac - this.b)/(2*this.a);
}
};
}
function elimateCoefficient(p0,p1,pos){
var s = p0[pos]/p1[pos];
for(var i=0;i<4;i++){
if (pos == i){
p1[i]= 0 ;
}else{
p1[i]*=s;
p1[i] -= p0[i];
}
}
return p1;
}
function clone(ar){
var ret = new Array(ar.length);
for(var i=0;i<ar.length;i++){
ret[i] = ar[i];
}
return ret;
}
function parseFormula(points){
var p0 = y_fx(points[0]);
var p1 = y_fx(points[1]);
var p2 = y_fx(points[2]);
var p01 = elimateCoefficient(clone(p0),clone(p1),0);
var p12 = elimateCoefficient(clone(p1),clone(p2),0);
var p012 = elimateCoefficient(clone(p01),clone(p12),1);
var formula = new Formula();
formula.c = p012[3]/p012[2];
formula.b = (p01[3] - p01[2]*formula.c)/p01[1];
formula.a = (p0[3] - p0[2]*formula.c - p0[1]*formula.b)/p0[0];
return formula;
}
function y_fx(p){
return [Math.pow(p[0],2),p[0],1,p[1]];
}