Class: Vec2

JustMath. Vec2

Represents a two dimensional vector. Vector operations always affect the initial Vec2 instance and return the instance itself for chaining. So use JustMath.Vec2#clone where necessary. This is done to reduce the allocation footprint slightly.

new Vec2(vOrX, y)

Constructs a new Vec2.

Parameters:
Name Type Argument Description
vOrX JustMath.Vec2 | number

Other Vec2 to copy or X coordinate

y number <optional>

Y coordinate if vOrX is X coordinate

Members

x :number

X coordinate.

y :number

Y coordinate.

Methods

add(vOrX, y) → {JustMath.Vec2}

Adds a value to this Vec2.

Parameters:
Name Type Argument Description
vOrX JustMath.Vec2 | number

Other Vec2 or X coordinate

y number <optional>

Y coordinate if vOrX is X coordinate

Returns:

This Vec2

Type
JustMath.Vec2

clone() → {JustMath.Vec2}

Clones this Vec2.

Returns:

Cloned Vec2

Type
JustMath.Vec2

copy() → {JustMath.Vec2}

Copies this Vec2. This is an alias of JustMath.Vec2#clone.

Returns:

Copied Vec2

Type
JustMath.Vec2

dir() → {number}

Calculates the direction of this Vec2. This operation requires a call to JustMath.atan2.

Returns:

Direction in radians

Type
number

dist(b) → {number}

Calculates the distance between this and another Vec2. This operation requires a call to JustMath.sqrt.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

Distance

Type
number

distSq(b) → {number}

Calculates the squared distance between this and another Vec2.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

Squared distance

Type
number

dot(b) → {number}

Calculates the dot product of this and another Vec2.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

Dot product

Type
number

equals(b) → {boolean}

Tests if this Vec2 equals another Vec2.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

true if equal, false otherwise

Type
boolean

getX() → {number}

Gets the X coordinate of this Vec2.

Returns:

X coordinate

Type
number

getXY() → {{x: number, y: number}}

Gets the coordinate payload of this Vec2.

Returns:

Coordinate payload

Type
{x: number, y: number}

getY() → {number}

Gets the Y coordinate of this Vec2.

Returns:

Y coordinate

Type
number

inRect(p1, p2) → {boolean}

Tests if this Vec2 is contained in the rectangle created between p1 and p2.

Parameters:
Name Type Description
p1 JustMath.Vec2
p2 JustMath.Vec2
Returns:

true if contained, else false

Type
boolean

inv() → {JustMath.Vec2}

Inverts this Vec2.

Returns:

This Vec2

Type
JustMath.Vec2

lerp(p, percent) → {JustMath.Vec2}

Interpolates the point between this and another point (in that direction) at the given percentage.

Parameters:
Name Type Description
p JustMath.Vec2

Other point

percent number

Percentage

Returns:

This Vec2

Type
JustMath.Vec2

mag() → {number}

Calculates the magnitude of this Vec2. This operation requires a call to JustMath.sqrt.

Returns:

Magnitude

Type
number

magSq() → {number}

Calculates the squared magnitude of this Vec2.

Returns:

Squared magnitude

Type
number

norm() → {JustMath.Vec2}

Normalizes this Vec2.

Returns:

This Vec2

Type
JustMath.Vec2

ort() → {JustMath.Vec2}

Makes this Vec2 an orthogonal of itself by setting x=-y and y=x.

Returns:

This Vec2

Type
JustMath.Vec2

project(b) → {JustMath.Vec2}

Projects this Vec2 on another Vec2.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

This Vec2

Type
JustMath.Vec2

reflect(n) → {JustMath.Vec2}

Reflects this Vec2 from another Vec2.

Parameters:
Name Type Description
n JustMath.Vec2

Vector to reflect from

Returns:

This Vec2

Type
JustMath.Vec2

reflectAndScale(n, projectFactor, rejectFactor) → {JustMath.Vec2}

Reflects this Vec2 from another Vec2 and scales the projected and reflected component by the given factors.

Parameters:
Name Type Description
n JustMath.Vec2

Vector to reflect from

projectFactor number

Projected component factor

rejectFactor number

Rejected component factor

Returns:

This Vec2

Type
JustMath.Vec2

reject(b) → {JustMath.Vec2}

Rejects this Vec2 from another Vec2.

Parameters:
Name Type Description
b JustMath.Vec2

Other Vec2

Returns:

This Vec2

Type
JustMath.Vec2

rotate(theta) → {JustMath.Vec2}

Rotates this Vec2 by the given angle. This operation requires a call to JustMath.sin and JustMath.cos.

Parameters:
Name Type Description
theta number

Rotation angle in radians

Returns:

This Vec2

Type
JustMath.Vec2

scale(factor) → {JustMath.Vec2}

Scales this Vec2 by a factor.

Parameters:
Name Type Description
factor number

Scaling factor

Returns:

This Vec2

Type
JustMath.Vec2

set(vOrX, y) → {JustMath.Vec2}

Sets the coordinates of this Vec2.

Parameters:
Name Type Argument Description
vOrX JustMath.Vec2 | number

Other Vec2 or X coordinate

y number <optional>

Y coordinate if vOrX is X coordinate

Returns:

This Vec2

Type
JustMath.Vec2

sub(vOrX, y) → {JustMath.Vec2}

Subtracts a value from this Vec2.

Parameters:
Name Type Argument Description
vOrX JustMath.Vec2 | number

Other Vec2 or X coordinate

y number <optional>

Y coordinate if vOrX is X coordinate

Returns:

This Vec2

Type
JustMath.Vec2

toString() → {string}

Gets a string representation of this Vec2.

Returns:

String representation as of "Vec2(x/y)"

Type
string

<static> det(v1, v2) → {number}

Calculates the determinant of the matrix [v1,v2].

Parameters:
Name Type Description
v1 JustMath.Vec2

Vector 1

v2 JustMath.Vec2

Vector 2

Returns:

Determinant of the matrix [v1,v2]

Type
number