Module vec2
A 2 component vector.
Functions
new (x, y) | The public constructor. |
from_cartesian (radius, theta) | Convert point from polar to cartesian. |
clone (a) | Clone a vector. |
add (a, b) | Add two vectors. |
sub (a, b) | Subtract one vector from another. |
mul (a, b) | Multiply a vector by another vector. |
div (a, b) | Divide a vector by another vector. |
normalize (a) | Get the normal of a vector. |
trim (a, len) | Trim a vector to a given length. |
cross (a, b) | Get the cross product of two vectors. |
dot (a, b) | Get the dot product of two vectors. |
len (a) | Get the length of a vector. |
len2 (a) | Get the squared length of a vector. |
dist (a, b) | Get the distance between two vectors. |
dist2 (a, b) | Get the squared distance between two vectors. |
scale (a, b) | Scale a vector by a scalar. |
rotate (a, phi) | Rotate a vector. |
perpendicular (a) | Get the perpendicular vector of a vector. |
angle_to (a, b) | Signed angle from one vector to another. |
angle_between (a, b) | Unsigned angle between two vectors. |
lerp (a, b, s) | Lerp between two vectors. |
unpack (a) | Unpack a vector into individual components. |
component_min (a, b) | Return the component-wise minimum of two vectors. |
component_max (a, b) | Return the component-wise maximum of two vectors. |
is_vec2 (a) | Return a boolean showing if a table is or is not a vec2. |
is_zero (a) | Return a boolean showing if a table is or is not a zero vec2. |
has_nan (a) | Return whether either value is NaN |
to_polar (a) | Convert point from cartesian to polar. |
to_string (a) | Return a formatted string. |
Tables
vec2 | Constants |
Functions
- new (x, y)
-
The public constructor.
Parameters:
- x Can be of three types: number X component table {x, y} or {x = x, y = y} scalar to fill the vector eg. {x, x}
- y number Y component
Returns:
-
vec2
out
- from_cartesian (radius, theta)
-
Convert point from polar to cartesian.
Parameters:
- radius number Radius of the point
- theta number Angle of the point (in radians)
Returns:
-
vec2
out
- clone (a)
-
Clone a vector.
Parameters:
- a vec2 Vector to be cloned
Returns:
-
vec2
out
- add (a, b)
-
Add two vectors.
Parameters:
Returns:
-
vec2
out
- sub (a, b)
-
Subtract one vector from another.
Order: If a and b are positions, computes the direction and distance from b
to a.
Parameters:
Returns:
-
vec2
out
- mul (a, b)
-
Multiply a vector by another vector.
Component-size multiplication not matrix multiplication.
Parameters:
Returns:
-
vec2
out
- div (a, b)
-
Divide a vector by another vector.
Component-size inv multiplication. Like a non-uniform scale().
Parameters:
Returns:
-
vec2
out
- normalize (a)
-
Get the normal of a vector.
Parameters:
- a vec2 Vector to normalize
Returns:
-
vec2
out
- trim (a, len)
-
Trim a vector to a given length.
Parameters:
- a vec2 Vector to be trimmed
- len number Length to trim the vector to
Returns:
-
vec2
out
- cross (a, b)
-
Get the cross product of two vectors.
Order: Positive if a is clockwise from b. Magnitude is the area spanned by
the parallelograms that a and b span.
Parameters:
Returns:
-
number
magnitude
- dot (a, b)
-
Get the dot product of two vectors.
Parameters:
Returns:
-
number
dot
- len (a)
-
Get the length of a vector.
Parameters:
- a vec2 Vector to get the length of
Returns:
-
number
len
- len2 (a)
-
Get the squared length of a vector.
Parameters:
- a vec2 Vector to get the squared length of
Returns:
-
number
len
- dist (a, b)
-
Get the distance between two vectors.
Parameters:
Returns:
-
number
dist
- dist2 (a, b)
-
Get the squared distance between two vectors.
Parameters:
Returns:
-
number
dist
- scale (a, b)
-
Scale a vector by a scalar.
Parameters:
- a vec2 Left hand operand
- b number Right hand operand
Returns:
-
vec2
out
- rotate (a, phi)
-
Rotate a vector.
Parameters:
- a vec2 Vector to rotate
- phi number Angle to rotate vector by (in radians)
Returns:
-
vec2
out
- perpendicular (a)
-
Get the perpendicular vector of a vector.
Parameters:
- a vec2 Vector to get perpendicular axes from
Returns:
-
vec2
out
- angle_to (a, b)
-
Signed angle from one vector to another.
Rotations from +x to +y are positive.
Parameters:
Returns:
-
number
angle in (-pi, pi]
- angle_between (a, b)
-
Unsigned angle between two vectors.
Directionless and thus commutative.
Parameters:
Returns:
-
number
angle in [0, pi]
- lerp (a, b, s)
-
Lerp between two vectors.
Parameters:
Returns:
-
vec2
out
- unpack (a)
-
Unpack a vector into individual components.
Parameters:
- a vec2 Vector to unpack
Returns:
- number x
- number y
- component_min (a, b)
-
Return the component-wise minimum of two vectors.
Parameters:
Returns:
-
vec2
A vector where each component is the lesser value for that component between the two given vectors.
- component_max (a, b)
-
Return the component-wise maximum of two vectors.
Parameters:
Returns:
-
vec2
A vector where each component is the lesser value for that component between the two given vectors.
- is_vec2 (a)
-
Return a boolean showing if a table is or is not a vec2.
Parameters:
- a vec2 Vector to be tested
Returns:
-
boolean
is_vec2
- is_zero (a)
-
Return a boolean showing if a table is or is not a zero vec2.
Parameters:
- a vec2 Vector to be tested
Returns:
-
boolean
is_zero
- has_nan (a)
-
Return whether either value is NaN
Parameters:
- a vec2 Vector to be tested
Returns:
-
boolean
if x or y is nan
- to_polar (a)
-
Convert point from cartesian to polar.
Parameters:
- a vec2 Vector to convert
Returns:
- number radius
- number theta
- to_string (a)
-
Return a formatted string.
Parameters:
- a vec2 Vector to be turned into a string
Returns:
-
string
formatted