This is a simple Vector class I have written.

Please provide suggestions on what functions(methods?) to add to it or how to optimize it.

Thanks a bunch,
Jake

code:

//immutable Vector2D Class
class Vector2D
{
private var myComps : Object; //x and y components of the vector

function Vector2D(xComp : Number, yComp : Number)
{
myComps = new Object();
myComps.x = xComp;
myComps.y = yComp;
}

//return a new vector that is the vector sum of this and that
public function vectorSum(that : Vector2D) : Vector2D
{
return new Vector2D(myComps.x + that.myComps.x, myComps.y + that.myComps.y);
}

//return a new vector that is this subtracted with that
public function subtract(that : Vector2D) : Vector2D
{
return new Vector2D(myComps.x - that.myComps.x, myComps.y - that.myComps.y);
}

//return a new vector that is this multiplied by a factor
public function multiply(factor : Number) : Vector2D
{
return new Vector2D(myComps.x * factor, myComps.y * factor);
}

//The dot product is often used to calculate the cosine of the angle between two vectors.
public function dotProduct(that : Vector2D) : Number
{
return (myComps.x * that.myComps.x) + (myComps.y * that.myComps.y);
}

//The cross product of two vectors is a vector perpendicular to both of the two vectors
//returns the _length_ of the cross product of the two vectors
//useful for torque, magnetics, etc...
public function crossProductLength(that : Vector2D) : Number
{
return (myComps.x + that.myComps.y) - (that.myComps.x * myComps.y);
}

//return a new vector that is the reverse of this
public function reverse() : Vector2D
{
return new Vector2D(-myComps.x, -myComps.y);
}

//get the current angle of the vector travel (in radians)
public function angle() : Number
{
return Math.atan( myComps.y / myComps.x );
}

//return the angle between the two vectors (radians)
public function angleBetween(that : Vector2D) : Number
{
return Math.acos( dotProduct(that) / (magnitude() * that.magnitude()) );
}

//get the current magnitude of the vector
public function magnitude() : Number
{
return Math.sqrt( (myComps.x * myComps.x) + (myComps.y * myComps.y) );
}

//return the unit vector of this vector
public function unitVector() : Vector2D
{
return new Vector2D( myComps.x / magnitude(), myComps.y / magnitude() );
}

//access the x and y properties
public function getX() : Number
{
return myComps.x;
}

public function getY() : Number
{
return myComps.y;
}

//string representation
public function toString() : String
{
return myComps.x + " " + myComps.y;
}
}