-
Notifications
You must be signed in to change notification settings - Fork 0
Vector2
The root of all that is good!
Vector2
is a supremely useful class to represent a 2D Cartsian point in space. Here are a list of methods and properties.
constructor(x: number, y: number)
new Vector2(3, 4)
Or, with the vec2
helper function
vec2(3, 4)
add(other: Vector2): Vector2
Adds the two vectors together with vector addition.
Examples
-
vec2(2, 4).add(vec2(2, 4))
returnsVector2 { x: 4, y: 8 }
subtract(other: Vector2): Vector2
Subtracts other
from `this with vector subtraction.
Examples
-
vec2(2, 4).subtract(vec2(1, 2))
returnsVector2 { x: 1, y: 2 }
divide(n: number): Vector2
Divides the Vector2
by the scalar n
.
Examples
-
vec2(2, 4).divide(2)
returnsVector2 { x: 1, y: 2 }
multiply(n: number): Vector2
Multiplies the Vector2
by the scalar n
.
Examples
-
vec2(2, 4).multiply(2)
returnsVector2 { x: 4, y: 8 }
Alias for multiply
Vector2.mix(a: Vector2, b: Vector2, mix: number): Vector2
Linearly interpolates between vectors a
and b
. This method might be called lerp
in other frameworks.
Examples
-
Vector2.mix(vec2(10, 0), vec2(0, 0), 0.5)
returnsVector2 { x: 5, y: 0}
distanceTo(other: Vector2): number
Returns the Euclidean distance to the other vector.
Examples
-
vec2(0, 0).distanceTo(vec2(3, 4))
returns5
lenght(): number
Returns the Euclidean length of the vector.
Examples
-
vec2(3, 4).length()
returns5
dot(other: Vector2): Vector2
Returns the dot product of the two vectors.
Examples
-
vec2(2, 3).dot(vec2(3, 4))
returnsVector2 { x: 6, y: 12 }
angleTo(other: Vector2): Radians
Returns the angle made between the two vectors.
Examples
-
vec2(2, 0).angleTo(vec2(0, 2))
returns-Math.PI / 2
Vector2.midpoint(a: Vector2, b: Vector2): Vector2
Returns a vector at the midpoint of a
and b
.
Examples
-
Vector2.midpoint(vec2(0, 0), vec2(4, 4))
returnsVector2 { x: 2, y: 2 }
Vector2.random(xMin: number, xMax: number, yMin: number, yMax: number, rng: Rng): Vector2
Returns a random point in the provided bounds.
Examples
-
Vector2.random(0, 10, 15, 20, rng)
returns someVector
withx
in range[0, 10)
andy
in range[15, 20)
Vector2.randomInCircle(circle: Circle, rng: Rng): Vector2
Returns a random point in the provided circle.
Examples
-
Vector2.randomInCircle(circle({ center: vec2(6, 8), radius: 5 }), rng)
returns someVector2
within a distance of5
from the point centered at(6, 8)
Vector2.fromAngle(angle: number): Vector2
Returns a Vector2
with length 1
pointing in direction of angle
Examples
-
Vector2.fromAngle(0)
returnsVector2 { x: 1, y: 0 }
-
Vector2.fromAngle(Math.PI)
returnsVector2 { x: -1, y: 0 }
-
Vector2.fromAngle(Math.PI / 2)
returnsVector2 { x: 0, y: 1 }
-
Vector2.fromAngle(-Math.PI / 2)
returnsVector2 { x: 0, y: -1 }
jitter(amount: number, rng): Vector2
Returns a new Vector2
with x
and y
offset by a random amount in range [x - amount, x + amount)
, [y - amount, y + amount)
Examples
-
vec2(2, 3).jitter(0.5, rng)
returns someVector2
withx
in range[1.5, 2.5)
andy
in range[2.5, 3.5)
eq(other: Vector2): boolean
A simple value-equality check. Returns true
when the x
and y
values are equivalent.
Examples
-
vec2(3, 3).eq(vec2(3, 3))
returnstrue
-
vec2(3, 3).eq(vec2(3, 4))
returnsfalse