Class: Vector

Grape2D. Vector

new Vector(x, y)

Describes a math vector in the cartesian space (2D). This is also very useful and may be used to represent points.
Parameters:
Name Type Argument Description
x number <optional>
The x component
y number <optional>
The y component
Source:

Members

<private, static> STATIC_TYPE :string

Type as a string.
Type:
  • string
Source:

<private> x :number

The x component. The default value is 0.
Type:
  • number
Source:

<private> y :number

The y component. The default value is 0.
Type:
  • number
Source:

Methods

<static> createFromAngle(angle, magnitude) → {Grape2D.Vector}

Creates a vector from an angle and magnitude.
Parameters:
Name Type Description
angle number angle of the vector against the x axis.
magnitude number magnitude (length) of the vector.
Source:
Returns:
vector with the given angle and magnitude.
Type
Grape2D.Vector

<static> createFromPoints(a, b) → {Grape2D.Vector}

Creates a vector from two points (points are represented as vectors).
Parameters:
Name Type Description
a Grape2D.Vector One point.
b Grape2D.Vector Another point.
Source:
Returns:
Vector with direction from a to b.
Type
Grape2D.Vector

<static> lerp(start, end, prc) → {Grape2D.Vector}

Linear interpolation between two vectors.
Parameters:
Name Type Description
start Grape2D.Vector Start interpolation position.
end Grape2D.Vector End interpolation position.
prc number Percentage of the interpolation.
Source:
Returns:
Interpolated vector.
Type
Grape2D.Vector

add(vector) → {Grape2D.Vector}

Adds the components of another vector to this.
Parameters:
Name Type Description
vector Grape2D.Vector The vector to add.
Source:
Returns:
This vector.
Type
Grape2D.Vector

clone() → {Grape2D.Vector}

Creates a new vector with the same components.
Source:
Returns:
a new vector with the same components as this one.
Type
Grape2D.Vector

distanceTo(vector) → {number}

Calculates the distance between this and another vector
Parameters:
Name Type Description
vector Grape2D.Vector The other vector.
Source:
Returns:
The distance.
Type
number

divideByScalar(scalar) → {Grape2D.Vector}

Divides components by a scalar.
Parameters:
Name Type Description
scalar number The number to divide.
Source:
Returns:
This vector.
Type
Grape2D.Vector

dot(vector) → {number}

Gets the dot product of this and another vector.
Parameters:
Name Type Description
vector Grape2D.Vector Another vector.
Source:
Returns:
The dot product.
Type
number

equals(vector) → {boolean}

Checks if the components of one vector are equal to the components to another one.
Parameters:
Name Type Description
vector Grape2D.Vector The other vector.
Source:
Returns:
True if they're components are not equal.
Type
boolean

getAngle() → {number}

Gets the angle that the vector makes with the x axis.
Source:
Returns:
The angle.
Type
number

getMagnitude() → {number}

Gets the magnitude (length) of a vector.
Source:
Returns:
The magnitude of the vector.
Type
number

getStaticType() → {string}

Gets the type of the object. This is used by the collision dispatcher.
Source:
Returns:
Type.
Type
string

getX() → {number}

Gets the x component of the vector.
Source:
Returns:
The x component.
Type
number

getY() → {number}

Gets the y component of the vector.
Source:
Returns:
The y component.
Type
number

isParallelTo(vector) → {boolean}

Checks if two vectors are parallel.
Parameters:
Name Type Description
vector Grape2D.Vector vector to check.
Source:
Returns:
true if the vector is parallel to this one, false otherwise.
Type
boolean

isZero() → {boolean}

Gets if both x and y coordinates are zero.
Source:
Returns:
True if the coordinates are zero.
Type
boolean

length()

{@see Grape2D.Vector.getMagnitude}
Source:

lengthSquared() → {number}

Gets the length of the vector, before the calculation of its square root.
Source:
Returns:
The length squared.
Type
number

multiplyByScalar(scalar) → {Grape2D.Vector}

Multiplies components by a scalar.
Parameters:
Name Type Description
scalar number The number to multiply.
Source:
Returns:
This vector.
Type
Grape2D.Vector

negate() → {Grape2D.Vector}

Inverts the components of the vector. It's the same as multiply by -1.
Source:
Returns:
This vector.
Type
Grape2D.Vector

normalize() → {Grape2D.Vector}

Normalizes the vector. So that each component have a value between 0 and 1.
Source:
Returns:
This vector.
Type
Grape2D.Vector

project(vector) → {Grape2D.Vector}

Projects this vector into other. This operation don't changes the values of the objects.
Parameters:
Name Type Description
vector Grape2D.Vector The vector to project onto.
Source:
Returns:
The vector resulting from the projection.
Type
Grape2D.Vector

reset() → {Grape2D.Vector}

Resets the vector coordinates to 0.
Source:
Returns:
This vector.
Type
Grape2D.Vector

rightNormal() → {Grape2D.Vector}

Calculates the right normal of the vector.
Source:
Returns:
The right normal vector.
Type
Grape2D.Vector

set(vector) → {Grape2D.Vector}

Sets this vector with the same components of another one.
Parameters:
Name Type Description
vector Grape2D.Vector The vector to copy.
Source:
Returns:
This vector.
Type
Grape2D.Vector

setX(x)

Sets the x component of the vector.
Parameters:
Name Type Description
x number The new value.
Source:

setY(y)

Sets the y component of the vector.
Parameters:
Name Type Description
y number The new value.
Source:

sqDistanceTo(vector) → {number}

Calculates the squared distace between this and another vector.
Parameters:
Name Type Description
vector Grape2D.Vector The other vector.
Source:
Returns:
The distance squared.
Type
number

sub(vector) → {Grape2D.Vector}

Subtracts the components of another vector to this.
Parameters:
Name Type Description
vector Grape2D.Vector The vector to subtract.
Source:
Returns:
This vector.
Type
Grape2D.Vector

toString() → {string}

Creates a string for this class.
Source:
Returns:
A string representing this class.
Type
string

use(fn) → {Grape2D.Vector}

Applies the result of a given function, where the component is an argument, to the respective component. This can be useful to minimize code or just simplify it. As an example, someVector.use(Grape.Math.sqrt)
Parameters:
Name Type Description
fn Function A function that receives a number and returns a number.
Source:
Returns:
This vector.
Type
Grape2D.Vector

xDistanceTo(vector) → {number}

Positive distance between the x coordinates this vector an another one.
Parameters:
Name Type Description
vector Grape2D.Vector A vector.
Source:
Returns:
Positive distance of the x coordinate.
Type
number

yDistanceTo(vector) → {number}

Positive distance between the y coordinates this vector an another one.
Parameters:
Name Type Description
vector Grape2D.Vector A vector.
Source:
Returns:
Positive distance of the y coordinate.
Type
number