////////////////////////////////////////////////////////////////////////////
//                                                                        //
//          Copyright (C) 2010 ANSYS Canada Inc.  All Rights Reserved     //
//                                                                        //
// This file contains proprietary software licensed from ANSYS Inc.       //
// This header must remain in any source code despite modifications or    //
// enhancements by any party.                                             //
//                                                                        //
////////////////////////////////////////////////////////////////////////////
//
// $Workfile: bmUtilities.js $
// $Revision: 1.0 $
// $Date: 2010/11/08 14:09:00 $
// $Author: zsong $
//
// Decription: js block for general utilities
//
////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
//
// rountines and classes to support calculation
//
///////////////////////////////////////////////////////////////
function bmDeg2Rad( deg )
{
  return deg*Math.PI/180.0;
}

function bmEllipseTangPara(a, b, TangAng) // long axis, short axis, tangent angle in radian from -PI/2 to PI/2
{
  //atan2(y,x) and returns [-PI, PI]
  var angle = Math.atan2( -b*Math.cos(TangAng), a*Math.sin(TangAng) ); 
  return angle;
}

function bmVector(xp,yp)
{
  this.x = xp;
  this.y = yp;
  
  this.equal = function (v1)
  {
    this.x = v1.x;
    this.y = v1.y;
  }

  this.plus = function (v1)
  {
    this.x = this.x + v1.x;
    this.y = this.y + v1.y;
  }

  this.minus = function (v1)
  {
    this.x = this.x - v1.x;
    this.y = this.y - v1.y;
  }

  this.scale = function (s)
  {
    this.x = this.x * s;
    this.y = this.y * s;
  }

  this.unit = function ()
  {
    var len = this.mod();
    this.x = this.x/len;
    this.y = this.y/len;
  }

  this.mod = function ()
  {
    return Math.sqrt( this.x * this.x + this.y * this.y);
  }

  this.rot = function (ang) //rotate the vector, ccw in radian
  {
    var x2 = this.x * Math.cos(ang) - this.y * Math.sin(ang);
    var y2 = this.y * Math.cos(ang) + this.x * Math.sin(ang);
    
    this.x = x2;
    this.y = y2;
  }

  this.dot = function (v)
  {
    return this.x*v.x + this.y*v.y;
  }

  this.angle = function (v) //as radian
  {
    var m0 = this.mod();
    var m1 = v.mod();
    return Math.acos( this.dot(v)/(m0*m1) );
  }
}