The goog.math.Vec2 Class

goog.math.Vec2
> goog.math.Coordinate

goog.math.Vec2(x, y)

Class for a two-dimensional vector object and assorted functions useful for manipulating points.

x {number}
The x coordinate for the vector.
y {number}
The y coordinate for the vector.

.add(b)

Adds another vector to this vector in-place.

b {!goog.math.Coordinate}
The vector to add.
returns {!goog.math.Vec2}
This vector with {@code b} added.

.clone()

Returns a new copy of the coordinate.

returns {!goog.math.Vec2}
A new vector with the same coordinates as this one.

.difference(a, b)

Returns the difference between two vectors as a new Vec2.

a {!goog.math.Coordinate}
The first vector.
b {!goog.math.Coordinate}
The second vector.
returns {!goog.math.Vec2}
The difference vector.

.distance

Returns the distance between two vectors.

a {!goog.math.Coordinate}
The first vector.
b {!goog.math.Coordinate}
The second vector.
returns {number}
The distance.

.dot(a, b)

Returns the dot-product of two vectors.

a {!goog.math.Coordinate}
The first vector.
b {!goog.math.Coordinate}
The second vector.
returns {number}
The dot-product of the two vectors.

.equals(b)

Compares this vector with another for equality.

b {!goog.math.Vec2}
The other vector.
returns {boolean}
Whether this vector has the same x and y as the given vector.

.fromCoordinate(a)

Returns a new Vec2 object from a given coordinate.

a {!goog.math.Coordinate}
The coordinate.
returns {!goog.math.Vec2}
A new vector object.

.invert()

Reverses the sign of the vector. Equivalent to scaling the vector by -1.

returns {!goog.math.Vec2}
The inverted vector.

.lerp(a, b, x)

Returns a new Vec2 that is the linear interpolant between vectors a and b at scale-value x.

a {!goog.math.Coordinate}
Vector a.
b {!goog.math.Coordinate}
Vector b.
x {number}
The proportion between a and b.
returns {!goog.math.Vec2}
The interpolated vector.

.magnitude()

Returns the magnitude of the vector measured from the origin.

returns {number}
The length of the vector.

.normalize()

Normalizes the current vector to have a magnitude of 1.

returns {!goog.math.Vec2}
The normalized vector.

.random()

returns {!goog.math.Vec2}
A random vector inside the unit-disc.

.randomUnit()

returns {!goog.math.Vec2}
A random unit-length vector.

.rotate(angle)

Rotates this vector in-place by a given angle, specified in radians.

angle {number}
The angle, in radians.
returns {!goog.math.Vec2}
This vector rotated {@code angle} radians.

.rotateAroundPoint(v, axisPoint, angle)

Rotates a vector by a given angle, specified in radians, relative to a given axis rotation point. The returned vector is a newly created instance - no in-place changes are done.

v {!goog.math.Vec2}
A vector.
axisPoint {!goog.math.Vec2}
The rotation axis point.
angle {number}
The angle, in radians.
returns {!goog.math.Vec2}
The rotated vector in a newly created instance.

.scale(s)

Scales the current vector by a constant.

s {number}
The scale factor.
returns {!goog.math.Vec2}
The scaled vector.

.squaredDistance

Returns the squared distance between two vectors.

a {!goog.math.Coordinate}
The first vector.
b {!goog.math.Coordinate}
The second vector.
returns {number}
The squared distance.

.squaredMagnitude()

Returns the squared magnitude of the vector measured from the origin. NOTE(brenneman): Leaving out the square root is not a significant optimization in JavaScript.

returns {number}
The length of the vector, squared.

.subtract(b)

Subtracts another vector from this vector in-place.

b {!goog.math.Coordinate}
The vector to subtract.
returns {!goog.math.Vec2}
This vector with {@code b} subtracted.

.sum(a, b)

Returns the sum of two vectors as a new Vec2.

a {!goog.math.Coordinate}
The first vector.
b {!goog.math.Coordinate}
The second vector.
returns {!goog.math.Vec2}
The sum vector.

.toString()

Inherited from goog.math.Coordinate .

Returns a nice string representing the coordinate.

returns {string}
In the form (50, 73).

.x {number}

X-value

.y {number}

Y-value