// ---------------------------------------------------------------------------
// (c) 2003 Geotalk, Hans Martin Mohn
// ---------------------------------------------------------------------------
// Utility class for point and vector manipulation
//
// Dependencies:
//    None
//

gtPoint.prototype.set = function(x, y)
{
  this.x = x;
  this.y = y;
  return this;
}; // end set()



gtPoint.prototype.newCopy = function()
{
  return new gtPoint(this.x, this.y);
}; // end newCopy()



gtPoint.prototype.setFromString = function(p)
{
  var pt = p.split(",");
  this.x = parseFloat(pt[0]);
  this.y = parseFloat(pt[1]);
  return this;
}; // end setFromString()



gtPoint.prototype.setToSum = function(rhs)
{
  this.x += rhs.x;
  this.y += rhs.y;
  return this;
}; // end setToSum()



gtPoint.prototype.setToMiddle = function(rhs)
{
  this.x = (this.x + rhs.x) / 2.0;
  this.y = (this.y + rhs.y) / 2.0;
  return this;
}; // end setToMiddle()



gtPoint.prototype.setToMiddleOf = function(p1, p2)
{
  this.x = (p1.x + p2.x) / 2.0;
  this.y = (p1.y + p2.y) / 2.0;
  return this;
}; // end setToMiddleOf()



gtPoint.prototype.newMiddle = function(rhs)
{
  return new gtPoint((this.x + rhs.x) / 2.0, (this.y + rhs.y) / 2.0);
}; // end newMiddle()



gtPoint.prototype.length = function()
{
  return Math.sqrt(this.x * this.x + this.y * this.y);
}; // end length()



gtPoint.prototype.distanceTo = function(rhs)
{
  var h = rhs.x - this.x;
  var v = rhs.y - this.y;
  return Math.sqrt(h * h + v * v);
}; // end distanceTo()



gtPoint.prototype.dot = function(rhs)
{
  return this.x * rhs.x + this.y * rhs.y;
}; // end dot()



gtPoint.prototype.setToNormalized = function()
{
  var len = this.length();
  this.x /= len;
  this.y /= len;
  return this;
}; // end setToNormalized()



// rotate anticlockwise 90 deg
gtPoint.prototype.setToNormalOf = function(other)
{
  this.x = -other.y;
  this.y = other.x;
  return this;
}; // end setToNormalOf()



gtPoint.prototype.newNormal = function()
{
  return new gtPoint(-this.y, this.x);
}; // end newNormal()



gtPoint.prototype.angleTo = function(rhs)
{
  var v1 = Math.atan2(this.x, this.y);
  var v2 = Math.atan2(rhs.x,  rhs.y);
  var v = (v2 - v1) * 180.0 / Math.PI;
  return (v > 0) ? v : v + 360;
}; // end angleTo()



gtPoint.prototype.newVectorTo = function(rhs)
{
  return new gtPoint(rhs.x - this.x, rhs.y - this.y);
}; // end newVectorTo()



gtPoint.prototype.setToVector = function(a, b)
{
  this.x = b.x - a.x;
  this.y = b.y - a.y;
  return this;
}; // end setToVector()



gtPoint.prototype.setToReversed = function()
{
  this.x = -this.x;
  this.y = -this.y;
  return this;
}; // end setToReversed()



gtPoint.prototype.newReversed = function()
{
  return new gtPoint(-x, -y);
}; // end newReversed()



gtPoint.prototype.setToScaled = function(k)
{
  this.x *= k;
  this.y *= k;
  return this;
}; // end setToScaled()



gtPoint.prototype.newScaled = function(k)
{
  return new gtPoint(this.x * k, this.y * k);
}; // end newScaled()



// ---------------------------------------------------------------------------
// class gtPoint
// ---------------------------------------------------------------------------

function gtPoint(x, y)
{
  if (typeof(x) != "undefined" && typeof(y) != "undefined")
  {
    this.x = x;
    this.y = y;
  }
  else
  {
    this.x = 0;
    this.y = 0;
  }
  return this;
} // end gtPoint




