Vec

 

A 2D vector class for a single vector with two coordinates. These coordinates are JavaScript Number Objects, so they can be integers or floating point numbers, positive or negative.

Vector is not a class, it it used to represent any class that has x,y properties of type Number. b2Vec2 in the Box2D framework is one of these vector classes - it can be used in place of Vec whenever the Vector type is specified.

All of the functions in this class can accept vectors defined as Vector objects or as 2d coordinate systems (separate x,y values of type Number).

 

Constructors

These functions are used to instantiate new instances of the Vec class. Constructor functions must be preceded by the new keyword.

 

iio.Vec( Number x, Number y )

- Creates the vector ( x, y ). Undefined parameters are set to 0.

var v = new iio.Vec(1,1);
 

iio.Vec( Vector v )

- Creates a new vector with the same properties as the given one. If the parameter is left undefined, a new vector will be created at 0,0.

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(v1);
//v1 and v2 will be different objects with equivalent properties
 

Properties

The data contained in each Vec object.

 

.x :: Number

- The x coordinate of the vector.

var vector = new iio.Vec(1,3);
//get x coordinate
var x = vector.x;
//x will be set to 1
 

.y :: Number

- The y coordinate of the vector.

var vector = new iio.Vec(1,3);
//get y coordinate
var y = vector.y;
//y will be set to 3
 

Functions

These functions are available to all Vec objects. Most are mutator functions (they alter the properties of the object that calls them) and return this, to allow for cascading function calls.

 

.clone()

:: Returns Vec

- returns a new vector with the same properties as this one. Makes a hard copy of the object.

var v1 = new iio.Vec(1,1);
//get a clone vector
var v2 = v1.clone();
//v1 and v2 will point to two different objects
 

.toString()

:: Returns String

- returns a string with this vector's coordinates

var v1 = new iio.Vec(1,1);
//show the value of v1
alert(v1.toString())
 

.length()

:: Returns Number

- returns the length (magnitude) of this vector.

var v = new iio.Vec(10,10);
//get the length of v
var mag = v.length(); 
 

.normalize()

:: Returns this

- normalizes this vector (makes its length equal to one without altering its angle).

var v = new iio.Vec(10,10);
//normalize v
v.normalize();
 

.set( Vector v )

.set( Number x, Number y )

:: Returns this

- Sets this vector's coordinates to the given values or to those of the given vector.

var vector = new iio.Vec(1,1);
//set vector to new value
vector.set(10,10);
//or
var vector2 = new iio.Vec(10,10);
vector.set(vector2);
//vector will now have the coordinates (10,10)
 

.add( Vector v )

.add( Number x, Number y)

:: Returns this

- adds the given vector.

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(2,3);
//add v2 to v1
v1.add(v2);
//or
v1.add(2,3);
//v1 will have the coordinates (3, 4) after one add operation
 

.sub( Vector v )

.sub( Number x, Number y)

:: Returns this

- subtracts the given vector.

var v1 = new iio.Vec(10,10);
var v2 = new iio.Vec(2,3);
//subtract v2 from v1
v1.sub(v2);
//or
v1.sub(2,3);
//v1 will have the coordinates (8, 7) after one sub operation
 

.mult( Number factor )

:: Returns this

- multiplies this vector by the given factor.

var v = new iio.Vec(1,1);
//multiply v by 5
v.mult(5);
//v will have the coordinates (5,5)
 

.div( Number divisor )

:: Returns this

- divides this vector by the given divisor.

var v = new iio.Vec(10,10);
//divide v by 2
v.div(2);
//v will have the coordinates (5,5)
 

.dot( Vector v )

.dot( Number x, Number y)

:: Returns Number

- returns the dot product of this and the given vector. This function does not alter the properties of either object.

var v1 = new iio.Vec(10,10);
var v2 = new iio.Vec(2,2);
//get the dot product
var dP = v1.dot(v2);
//or
var dP = v2.dot(10,10);
 

.distance( Vector v )

.distance( Number x, Number y )

:: Returns Number

- returns the absolute distance between this vector and the one given.

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(10,10);
//get the distance between v1 and v2
var dist = v1.distance(v2);
//or
var dist = v1.distance(10,10)
 

.lerp( Vector v, Number weight )

.lerp( Number: x, y, weight )

:: Returns this

- interpolates the value of this vector with the given vector and weight. The weight must be between 0 and 1. This function performs the following operation: this = this + (v - this) * weight

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(10,10);
//move v1 halfway between its current position and v2's
v1.lerp(v2,0.5);
//or
v1.lerp(10,10,0.5)
 

Static Functions

These functions can be called without an instantiated Vec object. They are useful when creating new vectors with altered properties of existing ones. None of these functions alter the properties of the given vectors.

 

.toString( Vector v )

.toString( Number x, Number y )

:: Returns String

- returns a string of the given vector's coordinates.

var v = new iio.Vec(10,10);
//show the value of v
alert(Vec.toString(v))
 

.length( Vector v )

.length( Number x, Number y )

:: Returns Number

- returns the length (magnitude) of the given vector.

var v = new iio.Vec(10,10);
//get the length of v
var mag = iio.Vec.length(v);
//or
var mag = iio.Vec.length(10,10);
 

.normalize( Vector v )

.normalize( Number x, Number y )

:: Returns Vec

- returns a normalized copy of the given vector.

var v = new iio.Vec(10,10);
//get normalized v without altering v
var var v2 = iio.Vec.normalize(v);
//or create a normalized vector with int parameters
var var v3 = iio.Vec.normalize(10,10);
 

.add( Vector: v1, v2 )

.add( Vector v1, Number: x2, y2 )

.add( Number: x1, y1, Vector v2 )

.add( Number: x1, y1, x2, y2 )

:: Returns Vec

- returns a new vector equal to the sum of the two given vectors.

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(2,3);
//get a iio.vector equal to the sum of v1 and v2
var v3 = iio.Vec.add(v1,v2);
//or
var v3 = iio.Vec.add(v1,2,3);
//or
var v3 = iio.Vec.add(1,1,v2);
//or
var v3 = iio.Vec.add(1,1,2,3);
//v3 will have the coordinates (3, 4)
 

.sub( Vector: v1, v2 )

.sub( Vector v1, Number: x2, y2 )

.sub( Number: x1, y1, Vector v2 )

.sub( Number: x1, y1, x2, y2 )

:: Returns Vec

- returns a new vector equal to the first given vector subtracted by the second.

var v1 = new iio.Vec(10,10);
var v2 = new iio.Vec(2,3);
//get a iio.vector equal to the sum of v1-v2
var v3 = iio.Vec.sub(v1,v2);
//or
var v3 = iio.Vec.sub(v1,2,3);
//or
var v3 = iio.Vec.sub(10,10,v2);
//or
var v3 = iio.Vec.sub(10,10,2,3);
//v3 will have the coordinates (8,7)
 

.mult( Vector v1, Number factor )

.mult( Number: x1, y1, factor )

:: Returns Vec

- returns a new vector equal to the given vector multiplied by the given factor.

var v = new iio.Vec(1,1);
//get vector v*5
var v2 = iio.Vec.mult(v,5);
//or
var v2 = iio.Vec.mult(1,1,5);
//v will have the coordinates (5, 5)
 

.div( Vector v1, Number divisor )

.div( Number: x1, y1, divisor )

:: Returns Vec

- returns a new vector equal to the given vector divided given divisor.

var v = new iio.Vec(10,10);
//get vector v/2
var v2 = iio.Vec.div(v,2);
//or
var v2 = iio.Vec.div(10,10,2);
//v will have the coordinates (5,5)
 

.dot( Vector: v1, v2 )

.dot( Vector v1, Number: x2, y2 )

.dot( Number: x1, y1, Vector v2 )

.dot( Number: x1, y1, x2, y2 )

:: Returns Number

- returns the dot product of the given vectors.

var v1 = new iio.Vec(10,10);
var v2 = new iio.Vec(2,2);
//get the dot product of v1 and v2
var dP = iio.Vec.dot(v1,v2);
//or
var v3 = iio.Vec.dot(v1,2,2);
//or
var v3 = iio.Vec.dot(10,10,v2);
//or
var v3 = iio.Vec.dot(10,10,2,2);
 

.distance( Vector: v1, v2 )

.distance( Vector v1, Number: x2, y2 )

.distance( Number: x1, y1, Vector v2 )

.distance( Number: x1, y1, x2, y2 )

:: Returns Number

- returns the absolute distance between the given vectors.

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(10,10);
//get the distance between v1 and v2
var dist = iio.Vec.distance(v1,v2);
//or
var dist = iio.Vec.distance(v1,10,10);
//or
var dist = iio.Vec.distance(1,1,v2);
//or
var dist = iio.Vec.distance(1,1,10,10);
 

.lerp( Vector: v1, v2, Number weight )

.lerp( Vector v1, Number: x2, y2, weight )

.lerp( Number: x1, y1, Vector v2, Number weight )

.lerp( Number: x1, y1, x2, y2, weight )

:: Returns this

- returns a new vector that equals the interpolation of the given vectors and weight value. The weight must be between 0 and 1. This function performs the following operation: this = this + (v - this) * weight

var v1 = new iio.Vec(1,1);
var v2 = new iio.Vec(10,10);
//get a new vector halfway between v1 and v2
var v3 = iio.Vec.lerp(v1,v2,0.5);
//or
var v3 = iio.Vec.lerp(v1,10,10,0.5);
//or
var v3 = iio.Vec.lerp(1,1,v2,0.5);
//or
var v3 = iio.Vec.lerp(1,1,10,10,0.5);