A 2D vector class
new gamvas.Vector2D(x, y);
x | The x part of the vector (optional) |
y | The y part of the vector (optional) |
gamvas. | A 2D vector class |
Variables | |
x | The x direction of the vector, also used as coordinate value for some functions. |
y | The y direction of the vector, also used as coordinate value for some functions. |
Functions | |
length | Return the length of the vector in pixels. |
quickLength | Returns the non sqrt() length of the vector, which is faster to calculate as it’s real length. |
normalized | Returns the normalized vector of a vector. |
rotate | Returns a new vector with the current vector rotated a certain angle. |
substract | Returns a new vector subtracting vector v from current vector |
add | Returns a new vector adding vector v to the current vector |
difference | Returns a new vector holding the difference between vector v and the current vector |
copy | Returns a copy of the vector |
distance | Returns the distance between the current vector and vector v |
quickDistance | Returns a comparable distance between the current vector and vector v |
gamvas.Vector2D.prototype.length = function()
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
gamvas.Vector2D.prototype.quickLength = function()
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
gamvas.Vector2D.prototype.distance = function( v )
Returns the distance between the current vector and vector v
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
var vec1 = new gamvas.Vector2D(1, 1); var vec2 = new gamvas.Vector2D(2, 3); console.log('The vectors are '+vec1.distance(vec2)+' units away');
gamvas.Vector2D.prototype.quickDistance = function( v )
Returns a comparable distance between the current vector and vector v
This is faster then testing the distance with gamvas.Vector2D.distance but does not give the actual distance, only a comparable value
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'); }
The x direction of the vector, also used as coordinate value for some functions.
( x ) ? this.x
The y direction of the vector, also used as coordinate value for some functions.
( y ) ? this.y
Return the length of the vector in pixels.
gamvas.Vector2D.prototype.length = function()
Returns the non sqrt() length of the vector, which is faster to calculate as it’s real length.
gamvas.Vector2D.prototype.quickLength = function()
Returns the normalized vector of a vector.
gamvas.Vector2D.prototype.normalized = function()
Returns a new vector with the current vector rotated a certain angle.
gamvas.Vector2D.prototype.rotate = function( r )
Returns a new vector adding vector v to the current vector
gamvas.Vector2D.prototype.add = function( v )
Returns a new vector holding the difference between vector v and the current vector
gamvas.Vector2D.prototype.difference = function( v )
Returns a copy of the vector
gamvas.Vector2D.prototype.copy = function()
Returns the distance between the current vector and vector v
gamvas.Vector2D.prototype.distance = function( v )
Returns a comparable distance between the current vector and vector v
gamvas.Vector2D.prototype.quickDistance = function( v )