Module vec2
vec2: A simple 2-component vector
Functions
vec2 (x, y) | Create a new vector with components x, y: (Can also be used to duplicate a vector: vec2(v)) |
copy (v) | Create a copy of a vector: |
fromAngle (angle) | Create a unit (length 1) vector from an angle |
fromPolar (length, angle) | Create a vector from a polar form length and angle |
addnew (a, b) | Add two vectors (or numbers) to create a new vector |
subnew (a, b) | Subtract two vectors (or numbers) to create a new vector |
mulnew (a, b) | Multiply two vectors (or numbers) to create a new vector |
divnew (a, b) | Divide two vectors (or numbers) to create a new vector |
pownew (a, b) | Raise to power two vectors (or numbers) to create a new vector |
modnew (a, b) | Calculate modulo two vectors (or numbers) to create a new vector |
minnew (a, b) | Calculate minimum of elements to create a new vector |
maxnew (a, b) | Calculate maximum of elements to create a new vector |
random (mag) | Create a new vector in a uniformly random direction: |
dot (a, b) | return the dot product of two vectors: |
distance (a, b) | The distance between two vectors (two points) (The relative distance from a to b) |
anglebetween (a, b) | The angle between two vectors (two points) (The relative angle from self to v) |
Class vec2
vec2:set (x, y) | Set the components of a vector: |
vec2:add (v) | Add a vector (or number) to self (in-place) |
vec2:sub (v) | Subtract a vector (or number) to self (in-place) |
vec2:mul (v) | Multiply a vector (or number) to self (in-place) |
vec2:div (v) | Divide a vector (or number) to self (in-place) |
vec2:pow (v) | Raise to power a vector (or number) to self (in-place) |
vec2:mod (v) | Calculate modulo a vector (or number) to self (in-place) |
vec2:min (v) | Calculate minimum of elements (in-place) |
vec2:max (v) | Calculate maximum of elements (in-place) |
vec2:clip (lo, hi) | Constrain vector to range (in-place) |
vec2:clip (lo, hi) | Constrain vector to range to create a new vector |
vec2:relativewrap (dimx, dimy) | Determine shortest relative vector in a toroidal space |
vec2:relativewrapnew (dimx, dimy) | Create new vector as shortest relative vector in a toroidal space |
vec2:lerp (v, f) | interpolate from self to v by factor of f |
vec2:lerpnew (b, f) | create a vector from the linear interpolation of two vectors: |
vec2:normalize () | set the length of the vector to 1 (unit vector) (randomized direction if self length was zero) |
vec2:normalizenew () | return a normalized copy of the vector (randomized direction if self length was zero) |
vec2:limit (maximum) | Impose a maximum magnitude Rescales vector if greater than maximum |
vec2:limitnew (maximum) | Create a copy of a vector, limited to a maximum magnitude Rescales vector if greater than maximum |
vec2:setangle (a) | Rotate a vector to a specific angle: |
vec2:setanglenew (a) | Return a copy rotated to a specific angle: |
vec2:setmag (m) | Rescale a vector to a specific magnitude: |
vec2:setmagnew (m) | Return a vector copy rescaled to a specific magnitude: |
vec2:rotate (angle) | Rotate a vector by an angle |
vec2:rotatenew (angle) | Create a vector by rotating a vector by an angle |
vec2:randomize (mag) | Set to a vector of magnitude 1 in a uniformly random direction: |
vec2:length () | return the length of a vector (Can also use #vector) |
vec2:magSqr () | return the squared length of a vector |
vec2:angle () | Return the angle to the vector (direction) |
vec2:polar () | Return the magnitude and angle (polar form): |
vec2:cross (a, b) | Return the cross product of two vectors. |
vec2:unpack () | Return the components of the vector as values |
Functions
- vec2 (x, y)
-
Create a new vector with components x, y:
(Can also be used to duplicate a vector: vec2(v))
Parameters:
- x number or vector (optional, default 0)
- y number (optional, default 0)
- copy (v)
-
Create a copy of a vector:
Parameters:
- v vector
- fromAngle (angle)
-
Create a unit (length 1) vector from an angle
Parameters:
- angle in radians
- fromPolar (length, angle)
-
Create a vector from a polar form length and angle
Parameters:
- length magnitude
- angle in radians
- addnew (a, b)
-
Add two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- subnew (a, b)
-
Subtract two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- mulnew (a, b)
-
Multiply two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- divnew (a, b)
-
Divide two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- pownew (a, b)
-
Raise to power two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- modnew (a, b)
-
Calculate modulo two vectors (or numbers) to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- minnew (a, b)
-
Calculate minimum of elements to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- maxnew (a, b)
-
Calculate maximum of elements to create a new vector
Parameters:
- a vector or number
- b vector or number
Returns:
-
new vector
- random (mag)
-
Create a new vector in a uniformly random direction:
Parameters:
- mag magnitude (optional, default 1)
Returns:
-
new vector
- dot (a, b)
-
return the dot product of two vectors:
Parameters:
- a vector
- b vector
Returns:
-
dot product
- distance (a, b)
-
The distance between two vectors (two points)
(The relative distance from a to b)
Parameters:
- a vector to measure distance between
- b vector to measure distance between
Returns:
-
distance
- anglebetween (a, b)
-
The angle between two vectors (two points)
(The relative angle from self to v)
Parameters:
- a vector to measure angle between
- b vector to measure angle between
Returns:
-
angle
Class vec2
A 2-component vector- vec2:set (x, y)
-
Set the components of a vector:
Parameters:
- x component
- y component
Returns:
-
self
- vec2:add (v)
-
Add a vector (or number) to self (in-place)
Parameters:
- v number or vector to add
Returns:
-
self
- vec2:sub (v)
-
Subtract a vector (or number) to self (in-place)
Parameters:
- v number or vector to sub
Returns:
-
self
- vec2:mul (v)
-
Multiply a vector (or number) to self (in-place)
Parameters:
- v number or vector to mul
Returns:
-
self
- vec2:div (v)
-
Divide a vector (or number) to self (in-place)
Parameters:
- v number or vector to div
Returns:
-
self
- vec2:pow (v)
-
Raise to power a vector (or number) to self (in-place)
Parameters:
- v number or vector to pow
Returns:
-
self
- vec2:mod (v)
-
Calculate modulo a vector (or number) to self (in-place)
Parameters:
- v number or vector to mod
Returns:
-
self
- vec2:min (v)
-
Calculate minimum of elements (in-place)
Parameters:
- v number or vector limit
Returns:
-
self
- vec2:max (v)
-
Calculate maximum of elements (in-place)
Parameters:
- v number or vector limit
Returns:
-
self
- vec2:clip (lo, hi)
-
Constrain vector to range (in-place)
Parameters:
- lo vector or number minimum value
- hi vector or number minimum value
Returns:
-
self
- vec2:clip (lo, hi)
-
Constrain vector to range to create a new vector
Parameters:
- lo vector or number minimum value
- hi vector or number minimum value
Returns:
-
new vector
- vec2:relativewrap (dimx, dimy)
-
Determine shortest relative vector in a toroidal space
Parameters:
- dimx width of space (optional, default 1)
- dimy height of space (optional, default dimx)
Returns:
-
self
- vec2:relativewrapnew (dimx, dimy)
-
Create new vector as shortest relative vector in a toroidal space
Parameters:
- dimx width of space (optional, default 1)
- dimy height of space (optional, default dimx)
Returns:
-
new vector
- vec2:lerp (v, f)
-
interpolate from self to v by factor of f
Parameters:
- v vector
- f interpolation factor from self to v (0 = none, 1 = full)
Returns:
-
self
- vec2:lerpnew (b, f)
-
create a vector from the linear interpolation of two vectors:
Parameters:
- b vector
- f interpolation factor from a to b (0 = none, 1 = full)
Returns:
-
new vector
- vec2:normalize ()
-
set the length of the vector to 1 (unit vector)
(randomized direction if self length was zero)
Returns:
-
self
- vec2:normalizenew ()
-
return a normalized copy of the vector
(randomized direction if self length was zero)
Returns:
-
vector of length 1 (unit vector)
- vec2:limit (maximum)
-
Impose a maximum magnitude
Rescales vector if greater than maximum
Parameters:
- maximum maximum magnitude of vector
Returns:
-
self
- vec2:limitnew (maximum)
-
Create a copy of a vector, limited to a maximum magnitude
Rescales vector if greater than maximum
Parameters:
- maximum maximum magnitude of vector
Returns:
-
new vector
- vec2:setangle (a)
-
Rotate a vector to a specific angle:
Parameters:
- a new angle
Returns:
-
self
- vec2:setanglenew (a)
-
Return a copy rotated to a specific angle:
Parameters:
- a new angle
Returns:
-
new vector
- vec2:setmag (m)
-
Rescale a vector to a specific magnitude:
Parameters:
- m new magnitude
Returns:
-
self
- vec2:setmagnew (m)
-
Return a vector copy rescaled to a specific magnitude:
Parameters:
- m new magnitude
Returns:
-
new vector
- vec2:rotate (angle)
-
Rotate a vector by an angle
Parameters:
- angle in radians
Returns:
-
self
- vec2:rotatenew (angle)
-
Create a vector by rotating a vector by an angle
Parameters:
- angle in radians
Returns:
-
new vector
- vec2:randomize (mag)
-
Set to a vector of magnitude 1 in a uniformly random direction:
Parameters:
- mag magnitude (optional, default 1)
Returns:
-
self
- vec2:length ()
-
return the length of a vector
(Can also use #vector)
Returns:
-
length
- vec2:magSqr ()
-
return the squared length of a vector
Returns:
-
length
- vec2:angle ()
-
Return the angle to the vector (direction)
Returns:
-
angle (in radians)
- vec2:polar ()
-
Return the magnitude and angle (polar form):
Returns:
-
length, angle (in radians)
- vec2:cross (a, b)
-
Return the cross product of two vectors.
(Of course, the cross product only applies to 3D vectors. In this case, we return the magnitude of the cross product of vec3's where the z component is zero.)
Parameters:
- a vector
- b vector
Returns:
-
magnitude of 3D cross product
- vec2:unpack ()
- Return the components of the vector as values