Class twodee.Vector
A vector with two elements.
Defined in: Vector.js.
Constructor Attributes | Constructor Name and Description |
---|---|
twodee.Vector(x, y)
Constructs a new empty vector.
|
Field Attributes | Field Name and Description |
---|---|
<static> |
twodee.Vector.counter
Instance counter.
|
The X coordinate.
|
|
The Y coordinate.
|
Method Attributes | Method Name and Description |
---|---|
add(v)
Adds the coordinates of the specified vector to this one.
|
|
copy(v)
Returns a copy of this vector.
|
|
cross(v)
Creates the cross product of this vector and the specified one and stores
the result back into this vector.
|
|
dot(v)
Creates and returns the dot product of this vector and the specified one.
|
|
<static> |
twodee.Vector.fromJSON(data)
Creates a new vector instance with the data read from the
specified JSON object (with keys 'x', and 'y').
|
getAngle(v)
Calculate the angle between this vector and the specified one in radians.
|
|
Returns the length of the vector.
|
|
isZero()
Checks if this vector is a zero vector.
|
|
Converts this vector into its orthogonal vector and returns a reference to
it.
|
|
rotate(angle)
Rotates the vector by the specified angle.
|
|
scale(fx, fy)
Scales the vector with the specified factors.
|
|
set(x, y)
Sets the vector coordinates.
|
|
sub(v)
Subtracts the coordinates of the specified vector from this one.
|
|
toJSON()
Converts the vector into a JSON object with keys 'x' and 'y'.
|
|
toUnit()
Converts this vector into a unit vector with length 1.
|
|
transform(m)
Transforms this vector with the specified matrix.
|
Class Detail
twodee.Vector(x, y)
Constructs a new empty vector.
- Parameters:
- {number=} x
- The X coordinate (Optional)
- {number=} y
- The Y coordinate (Optional)
Field Detail
<static>
twodee.Vector.counter
Instance counter.
x
The X coordinate.
y
The Y coordinate.
Method Detail
{twodee.Vector}
add(v)
Adds the coordinates of the specified vector to this one.
- Parameters:
- {twodee.Vector} v
- The vector to add
- Returns:
- {twodee.Vector} This vector
{twodee.Vector}
copy(v)
Returns a copy of this vector.
- Parameters:
- {twodee.Vector=} v
- Optional target vector
- Returns:
- {twodee.Vector} A copy of this vector or the target vector
{twodee.Vector}
cross(v)
Creates the cross product of this vector and the specified one and stores
the result back into this vector.
- Parameters:
- {twodee.Vector} v
- The other vector
- Returns:
- {twodee.Vector} This vector
{number}
dot(v)
Creates and returns the dot product of this vector and the specified one.
- Parameters:
- {twodee.Vector} v
- The other vector
- Returns:
- {number} The dot product
<static>
{twodee.Vector}
twodee.Vector.fromJSON(data)
Creates a new vector instance with the data read from the
specified JSON object (with keys 'x', and 'y'). Returns null if data
was empty.
- Parameters:
- {{x:number|y:number}} data
- The vector as JSON object
- Returns:
- {twodee.Vector} The vector object or null if data was empty
{number}
getAngle(v)
Calculate the angle between this vector and the specified one in radians.
It returns always the shortest angle. If the angle is clock-wise then the
returned number is positive. If it's counter-clock-wise then the angle is
negative. So the return value of this method is always between -PI and +PI.
- Parameters:
- {twodee.Vector=} v
- The second vector. Optional. If not specified then the angle to the vector 0;1 is returned.
- Returns:
- {number} The angle between the two vectors, in radians
{number}
getLength()
Returns the length of the vector.
- Returns:
- {number} The vector length
{boolean}
isZero()
Checks if this vector is a zero vector.
- Returns:
- {boolean} True if vector is a zero vector, false if not
{twodee.Vector}
orthogonal()
Converts this vector into its orthogonal vector and returns a reference to
it.
- Returns:
- {twodee.Vector} The reference to this vector
{twodee.Vector}
rotate(angle)
Rotates the vector by the specified angle.
- Parameters:
- {number} angle
- The rotation angle in anti-clockwise RAD.
- Returns:
- {twodee.Vector} This vector
{twodee.Vector}
scale(fx, fy)
Scales the vector with the specified factors.
- Parameters:
- {number} fx
- The X factor
- {number=} fy
- The Y factor (Optional. Defaults to fx)
- Returns:
- {twodee.Vector} This vector
{twodee.Vector}
set(x, y)
Sets the vector coordinates.
- Parameters:
- {number} x
- The X coordinate
- {number} y
- The Y coordinate
- Returns:
- {twodee.Vector} This vector
{twodee.Vector}
sub(v)
Subtracts the coordinates of the specified vector from this one.
- Parameters:
- {twodee.Vector} v
- The vector to subtract
- Returns:
- {twodee.Vector} This vector
{Object}
toJSON()
Converts the vector into a JSON object with keys 'x' and 'y'.
- Returns:
- {Object} The vector as a JSON object
{twodee.Vector}
toUnit()
Converts this vector into a unit vector with length 1.
- Returns:
- {twodee.Vector} This vector
{twodee.Vector}
transform(m)
Transforms this vector with the specified matrix.
- Parameters:
- {twodee.Matrix} m
- The matrix
- Returns:
- {twodee.Vector} This vector