Skip to main content

Math2

Math class for Vector2's.

Example

Add two Vector2's

const a: Vector2 = { x: 100, y: 100 };
const b: Vector2 = { x: 50, y: 50 };
const c = Math2.add(a, b);

Reference

Methods

magnitudeSquared

magnitudeSquared(p);

Returns the squared length of vector p

Parameters

NAMETYPE
pVector2

magnitude

magnitude(p);

Returns the length of vector p

Parameters

NAMETYPE
pVector2

normalize

normalize(p);

Returns p normalized, if length of p is 0 {x: 0, y: 0} is returned

Parameters

NAMETYPE
pVector2

dot

dot(a, b);

Returns the dot product between a and b

Parameters

NAMETYPE
aVector2
bVector2

subtract

subtract(a, b);

Returns a minus b

Parameters

NAMETYPE
aVector2
bVector2 | number

add

add(a, b);

Returns a plus b

Parameters

NAMETYPE
aVector2
bVector2 | number

multiply

multiply(a, b);

Returns a multiplied by b

Parameters

NAMETYPE
aVector2
bVector2 | number

divide

divide(a, b);

Returns a divided by b

Parameters

NAMETYPE
aVector2
bVector2 | number

rotate

rotate(point, origin, angle);

Rotates a point around a given origin by an angle in degrees

Returns the rotated point

Parameters

NAMETYPEDESCRIPTION
pointVector2Point to rotate
originVector2Origin of the rotation
anglenumberAngle of rotation in degrees

min

min(a, b);

Returns the min of a and b as a Vector

Parameters

NAMETYPE
aVector2
bVector2 | number

componentMin

min(a);

Returns the component wise minimum of a

Parameters

NAMETYPE
aVector2

max

max(a, b);

Returns the max of a and b as a Vector

Parameters

NAMETYPE
aVector2
bVector2 | number

componentMax

max(a);

Returns the component wise maximum of a

Parameters

NAMETYPE
aVector2

roundTo

roundTo(p, to);

Rounds p to the nearest value of to

Returns the rounded vector

Parameters

NAMETYPE
pVector2
toVector2

floorTo

floorTo(p, to);

Floors p to the nearest value of to

Returns the floored vector

Parameters

NAMETYPE
pVector2
toVector2

sign

sign(a);

Returns the component wise sign of a

Parameters

NAMETYPE
aVector2

abs

abs(a);

Returns the component wise absolute of a

Parameters

NAMETYPE
aVector2

pow

pow(a, b);

Returns a to the power of b

Parameters

NAMETYPE
aVector2
bVector2 | number

clamp

clamp(a, min max)

Returns a clamped between min and max

Parameters

NAMETYPE
aVector2
minnumber
maxnumber

boundingBox

boundingBox(points);

Returns an axis-aligned BoundingBox around an array of points

Parameters

NAMETYPE
pointsVector2[]

pointInPolygon

pointInPolygon(p, points);

Checks to see if a point is in a polygon

Returns true if the given point p is inside the polygon made by points

Parameters

NAMETYPE
pVector2
pointsVector2[]

compare

compare(a, b, threshold);

Returns true if the the distance between a and b is under threshold

Parameters

NAMETYPE
aVector2
bVector2
thresholdnumber

distance

distance(a, b);

Returns the euclidean distance between a and b

Parameters

NAMETYPE
aVector2
bVector2

lerp

lerp(a, b, alpha);

Returns the linear interpolation between a and b by alpha

Parameters

NAMETYPE
aVector2
bVector2
alphanumber

centroid

centroid(points);

Returns the centroid of the given points

Parameters

NAMETYPE
pointsVector2[]