Pages

Saturday, December 25, 2010

Linear Equation Algorithm

/**
 * Class for solving linear equation
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : December 25, 2010
 * @version : 1.0
 */


/**
 * Constructor service.
 * The equation supplied must be in the form:
 * ax+bx+c
 * This class is very limited in the for of the simple equation
 *
 * @param fa string the first linear equation
 * @param fb string the second linear equation
 */
function LinearEquation(fa,fb){
    this.fa = fa;
    this.fb = fb;

    var terma = this.fa.split(/[-+]/g);
    var a1 = terma[0].split(/x/)[0];
    var b1 = terma[1].split(/y/)[0];
    var c1 = terma[2];
    a1 = eval(a1);
    b1 = eval(b1);
    c1 = eval(c1);

    var termb = this.fb.split(/[+-]/g);
    var a2 = termb[0].split(/x/)[0];
    var b2 = termb[1].split(/y/)[0];
    var c2 = termb[2];
    a2 = eval(a2);
    b2 = eval(b2);
    c2 = eval(c2);
  
    this.x = ((b2*c1)-(c2*b1)) / ((a2*b1)-(b2*a1));
    this.y = (-c1 - a1*this.x) / b1;
}
/**
 * method to get the x,y value in array
 * @return array x,y value
 */
LinearEquation.prototype.getXY = function(){
    var data = new Array();
    data[0] = this.x;
    data[1] = this.y;
    return data;
};
/**
 * method to get x value
 * @return number x value
 */
LinearEquation.prototype.getX = function(){
    return this.x;
};
/**
 * method to get y value
 * @return number y value
 */
LinearEquation.prototype.getY = function(){
    return this.y;
};
           

No comments:

Post a Comment