/*
* Extra math functionality
* @author: Joseph 'Pcjoe' Florencio
* @date: 12-03-05
*/

/*
* Vector Class
*/
// Add Vectors
function addVector( vecAdd )
{
  this.x = this.x + vecAdd.x;
  this.y = this.y + vecAdd.y;
}

function createAddVector( vecAdd1, vecAdd2 )
{
  var newVector = new Vector();
  newVector.addVector(vecAdd1);
  newVector.addVector(vecAdd2);
  return newVector;
}

function addVectorXY( x, y )
{
  this.x = this.x + x;
  this.y = this.y + y;
}

// Subtract Vectors
function subVector( vecSub )
{
  this.x = this.x - vecSub.x;
  this.y = this.y - vecSub.y;
}

function createSubVector( vecAdd1, vecAdd2 )
{
  var newVector = new Vector();
  newVector.addVector(vecAdd1);
  newVector.subVector(vecAdd2);
  return newVector;
}

function subVectorXY( x, y )
{
  this.x = this.x - x;
  this.y = this.y - y;
}

// Get Length
function getLength()
{
  return Math.sqrt(this.x*this.x+this.y*this.y);
}

// Normalize Vector
function normalizeVector()
{
  var vecLength = this.getLength();
  if(vecLength > 0)
  {
    this.x = this.x / vecLength;
    this.y = this.y / vecLength;
  }
}

// Scale Vector (multiply)
function scaleVector( scale )
{
  this.x = this.x * scale;
  this.y = this.y * scale;
}

// Get dot product
function getDotProduct(vecDot)
{
  // Calculate and return dot product
  return ((this.x*vecDot.x+this.y*vecDot.y)/(this.getLength()*vecDot.getLength()));
}

// Inverse vector
function Inverse()
{
  this.x = -this.x;
  this.y = -this.y;
}

// Accessors
function getX() { return this.x; }
function getY() { return this.y; }
function setX(x){ this.x = x; }
function setY(y){ this.y = y; }
function setXY(x,y) { this.x = x; this.y = y; }
function setVector(vec) { this.x = vec.x; this.y = vec.y; }

// Vector constructor
function Vector()
{
  this.x=0;this.y=0;
  this.addVector = addVector;
  this.addVectorXY = addVectorXY;
  this.createAddVector = createAddVector;
  this.subVector = subVector;
  this.subVectorXY = subVectorXY;
  this.createSubVector = createSubVector;
  this.scaleVector = scaleVector;
  this.getLength = getLength;
  this.normalizeVector = normalizeVector;
  this.getDotProduct = getDotProduct;
  this.Inverse = Inverse;
  this.getX = getX;
  this.getY = getY;
  this.setX = setX;
  this.setY = setY;
  this.setXY = setXY;
  this.setVector = setVector;
}

// Inherited constructor, let setting of X and Y component
function VectorSet(x,y)
{   this.inherited = Vector;
    this.inherited();
    this.setX(x);
    this.setY(y);
}
