﻿// JScript File
if(!window.Sqr){
	function Sqr(arg) { /* return double; input double */
		/* Returns square of a double */
		return (arg*arg);
	}
}

if(!window.ArcCos){
	function ArcCos(arg) /* return double; input double */
	{
		/* Returns arccosine of argument */
		return(pio2-ArcSin(arg));
	}
}

function vector_t () {
	this.x = 0.0;
	this.y = 0.0;
	this.z = 0.0;
	this.w = 0.0; //magnitude compnent
	this.name = "vector_";
	
	this.Magnitude = function () {
		/* Calculates scalar magnitude of a vector_t */
		this.w = Math.sqrt(Sqr(this.x) + Sqr(this.y) + Sqr(this.z));
	}
	
	this.Scale_Vector = function (k) { 
		/* Multiplies the vector v1 by the scalar k */
		this.x *= k;
		this.y *= k;
		this.z *= k;
		this.w = Math.abs(k) * this.w;//this.Magnitude(); saves a little precision not using Magnitude()
	}
	
	this.Normalize = function () {
		/* Normalizes a vector */
		this.x /= this.w;
		this.y /= this.w;
		this.z /= this.w;
		this.w = 1.0;
	}
	
	this.Print = function () {
		/* Prints this vectors params to the current document */
		document.write(this.name+"["+this.x+", "+this.y+", "+this.z+", "+this.w+"]");
	}
}

function Vec_Add(v1, v2) {
	/* Adds vectors v1 and v2 together to produce v3 */
	var v3 = new vector_t();
	v3.name = v1.name+"+"+v2.name;
	v3.x = v1.x + v2.x;
	v3.y = v1.y + v2.y;
	v3.z = v1.z + v2.z;
	v3.Magnitude();
	return v3;
}

function Vec_Sub(v1, v2) {
	/* Subtracts vector v2 from v1 to produce v3 */
	var v3 = new vector_t();
	v3.name = v1.name+"-"+v2.name;
	v3.x = v1.x - v2.x;
	v3.y = v1.y - v2.y;
	v3.z = v1.z - v2.z;
	v3.Magnitude();
	return v3;
}

function Scalar_Multiply(k, v1) {
	/* Multiplies the vector v1 by the scalar k to produce the vector v2 */
	var v2 = new vector_t();
	v2.name = v1.name+"-ScalarMultiply";
	v2.x = k * v1.x;
	v2.y = k * v1.y;
	v2.z = k * v1.z;
	v2.w = Math.abs(k) * v1.w;
	return v2;
}

function Dot(v1, v2) {
	/* Returns the dot product of two vectors */
	return ( (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z) );
}

function Angle(v1, v2) {
	/* Calculates the angle between vectors v1 and v2 */
	v1.Magnitude();//verify that magnatude is set
	v2.Magnitude();//verify that magnatude is set
	return(ArcCos( Dot(v1,v2) / (v1.w * v2.w) ) );
}

function Cross(v1, v2)
{
	/* Produces cross product of v1 and v2, and returns in v3 */
	var v3 = new vector_t();
	v3.name = v1.name+"-Cross";
	v3.x = v1.y * v2.z - v1.z * v2.y;
	v3.y = v1.z * v2.x - v1.x * v2.z;
	v3.z = v1.x * v2.y - v1.y * v2.x;
	v3.Magnitude();
	return v3;
}