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
NAME | TYPE |
---|---|
p | Vector2 |
magnitude
magnitude(p);
Returns the length of vector p
Parameters
NAME | TYPE |
---|---|
p | Vector2 |
normalize
normalize(p);
Returns p
normalized, if length of p
is 0 {x: 0, y: 0}
is returned
Parameters
NAME | TYPE |
---|---|
p | Vector2 |
dot
dot(a, b);
Returns the dot product between a
and b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 |
subtract
subtract(a, b);
Returns a
minus b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
add
add(a, b);
Returns a
plus b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
multiply
multiply(a, b);
Returns a
multiplied by b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
divide
divide(a, b);
Returns a
divided by b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
rotate
rotate(point, origin, angle);
Rotates a point around a given origin by an angle in degrees
Returns the rotated point
Parameters
NAME | TYPE | DESCRIPTION |
---|---|---|
point | Vector2 | Point to rotate |
origin | Vector2 | Origin of the rotation |
angle | number | Angle of rotation in degrees |
min
min(a, b);
Returns the min of a
and b
as a Vector
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
componentMin
min(a);
Returns the component wise minimum of a
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
max
max(a, b);
Returns the max of a
and b
as a Vector
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
componentMax
max(a);
Returns the component wise maximum of a
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
roundTo
roundTo(p, to);
Rounds p
to the nearest value of to
Returns the rounded vector
Parameters
NAME | TYPE |
---|---|
p | Vector2 |
to | Vector2 |
floorTo
floorTo(p, to);
Floors p
to the nearest value of to
Returns the floored vector
Parameters
NAME | TYPE |
---|---|
p | Vector2 |
to | Vector2 |
sign
sign(a);
Returns the component wise sign of a
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
abs
abs(a);
Returns the component wise absolute of a
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
pow
pow(a, b);
Returns a
to the power of b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 | number |
clamp
clamp(a, min max)
Returns a
clamped between min
and max
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
min | number |
max | number |
boundingBox
boundingBox(points);
Returns an axis-aligned BoundingBox around an array of points
Parameters
NAME | TYPE |
---|---|
points | Vector2[] |
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
NAME | TYPE |
---|---|
p | Vector2 |
points | Vector2[] |
compare
compare(a, b, threshold);
Returns true if the the distance between a
and b
is under threshold
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 |
threshold | number |
distance
distance(a, b);
Returns the euclidean distance between a
and b
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 |
lerp
lerp(a, b, alpha);
Returns the linear interpolation between a
and b
by alpha
Parameters
NAME | TYPE |
---|---|
a | Vector2 |
b | Vector2 |
alpha | number |
centroid
centroid(points);
Returns the centroid of the given points
Parameters
NAME | TYPE |
---|---|
points | Vector2[] |