gamvas.Vector2D

Description

A 2D vector class

Constructor

new gamvas.Vector2D(x, y);

Parameters

xThe x part of the vector (optional)
yThe y part of the vector (optional)
Summary
gamvas.Vector2DA 2D vector class
Variables
xThe x direction of the vector, also used as coordinate value for some functions.
yThe y direction of the vector, also used as coordinate value for some functions.
Functions
lengthReturn the length of the vector in pixels.
quickLengthReturns the non sqrt() length of the vector, which is faster to calculate as it’s real length.
normalizedReturns the normalized vector of a vector.
rotateReturns a new vector with the current vector rotated a certain angle.
substractReturns a new vector subtracting vector v from current vector
addReturns a new vector adding vector v to the current vector
differenceReturns a new vector holding the difference between vector v and the current vector
copyReturns a copy of the vector
distanceReturns the distance between the current vector and vector v
quickDistanceReturns a comparable distance between the current vector and vector v

Variables

x

(x) ? this.x

Description

The x direction of the vector, also used as coordinate value for some functions.

y

(y) ? this.y

Description

The y direction of the vector, also used as coordinate value for some functions.

Functions

length

gamvas.Vector2D.prototype.length = function()

Description

Return the length of the vector in pixels.  This calculates the correct length of a vector which uses sqrt() and therefor is slow.  If you just want compare two vectors, see gamvas.Vector2D.quickLength

See

gamvas.Vector2D.quickLength

quickLength

gamvas.Vector2D.prototype.quickLength = function()

Description

Returns the non sqrt() length of the vector, which is faster to calculate as it’s real length.  Use it if you want to compare to vectors in length.  If you need the actual length of the vector, use the slower gamvas.Vector2D.length

See

gamvas.Vector2D.length

normalized

gamvas.Vector2D.prototype.normalized = function()

Description

Returns the normalized vector of a vector.  The normalized vector is the vector with length of 1.

Returns

gamvas.Vector2D

rotate

gamvas.Vector2D.prototype.rotate = function(r)

Description

Returns a new vector with the current vector rotated a certain angle.  The angle is in radians.

Parameters

rRotation in radians

Returns

gamvas.Vector2D

See

http://en.wikipedia.org/wiki/Radians

substract

Returns a new vector subtracting vector v from current vector

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = new gamvas.Vector2D(2, 2);
var subtracted = vec1.subtract(vec2);

add

gamvas.Vector2D.prototype.add = function(v)

Returns a new vector adding vector v to the current vector

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = new gamvas.Vector2D(2, 2);
var added = vec1.add(vec2);

difference

gamvas.Vector2D.prototype.difference = function(v)

Returns a new vector holding the difference between vector v and the current vector

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = new gamvas.Vector2D(2, 2);
var diff = vec1.difference(vec2);

copy

gamvas.Vector2D.prototype.copy = function()

Returns a copy of the vector

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = vec1.copy();
vec2.x += 1;
console.log(vec1.x); // will be 1
console.log(vec2.x); // will be 2

distance

gamvas.Vector2D.prototype.distance = function(v)

Returns the distance between the current vector and vector v

Description

This uses the sqrt() function which might be too slow, if you just need to compare several distances, see gamvas.Vector2D.quickDistance for a faster version

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = new gamvas.Vector2D(2, 3);
console.log('The vectors are '+vec1.distance(vec2)+' units away');

quickDistance

gamvas.Vector2D.prototype.quickDistance = function(v)

Returns a comparable distance between the current vector and vector v

Description

This is faster then testing the distance with gamvas.Vector2D.distance but does not give the actual distance, only a comparable value

Example

var vec1 = new gamvas.Vector2D(1, 1);
var vec2 = new gamvas.Vector2D(2, 3);
var vec3 = new gamvas.Vector2D(3, 4);
var distv1v2 = vec1.quickDistance(vec2);
var distv1v3 = vec1.quickDistance(vec3);
if (distv1v2 > distv1v3) {
   console.log('vector 1 is closer to vector 2');
} else {
   console.log('vector 1 is closer to vector 3');
}
(x) ? this.x
The x direction of the vector, also used as coordinate value for some functions.
(y) ? this.y
The y direction of the vector, also used as coordinate value for some functions.
gamvas.Vector2D.prototype.length = function()
Return the length of the vector in pixels.
gamvas.Vector2D.prototype.quickLength = function()
Returns the non sqrt() length of the vector, which is faster to calculate as it’s real length.
gamvas.Vector2D.prototype.normalized = function()
Returns the normalized vector of a vector.
gamvas.Vector2D.prototype.rotate = function(r)
Returns a new vector with the current vector rotated a certain angle.
gamvas.Vector2D.prototype.add = function(v)
Returns a new vector adding vector v to the current vector
gamvas.Vector2D.prototype.difference = function(v)
Returns a new vector holding the difference between vector v and the current vector
gamvas.Vector2D.prototype.copy = function()
Returns a copy of the vector
gamvas.Vector2D.prototype.distance = function(v)
Returns the distance between the current vector and vector v
gamvas.Vector2D.prototype.quickDistance = function(v)
Returns a comparable distance between the current vector and vector v
A 2D vector class
Close