Scenic.Math.Vector2 (Scenic v0.11.0-beta.0) View Source
A collection of functions to work with 2D vectors.
2D vectors are always two numbers in a tuple.
{3, 4}
{3.5, 4.7}
Link to this section Summary
Functions
Add two vectors together.
Given a list of vectors, find the {left, top, right, bottom} of the bounding box.
Clamp a vector to the space between two other vectors.
Calculates the cross product of two vectors.
Divide a vector by a scalar.
Calculates the dot product of two vectors.
A vector that points straight down by 1.
Determine if a vector is in the bounds (or clamp space) between two other vectors.
Determine if a vector is in the bounds (or clamp space) between two other vectors.
Invert a vector.
A vector that points left by 1.
Calculates the length of the vector.
Calculates the squared length of the vector.
Calculate the lerp of two vectors.
Find a new vector derived from the highest x
and y
from two given vectors.
Find a new vector derived from the lowest x
and y
from two given vectors.
Multiply a vector by a scalar.
Calculate the nlerp (normalized lerp) of two vectors.
Normalize a vector so it has the same angle, but a length of 1.
A vector that points to {1,1}.
Project a vector into the space defined by a matrix
A vector that points right by 1.
Round the values of a vector to the nearest integers.
Subtract one vector from another.
Truncate the values of a vector into integers.
A vector that points to {1,0}.
A vector that points to {0,1}.
A vector that points straight up by 1.
A vector that points to the origin.
Link to this section Functions
Specs
add(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Add two vectors together.
Parameters:
vector2_a
- the first vector to be addedvector2_b
- the second vector to be added
Returns: A new vector which is the result of the addition
Examples
iex> Scenic.Math.Vector2.add({1.0, 5.0}, {3.0, 3.0})
{4.0, 8.0}
Specs
bounds(vectors :: nil | [Scenic.Math.vector_2()]) :: {left :: number(), top :: number(), right :: number(), bottom :: number()}
Given a list of vectors, find the {left, top, right, bottom} of the bounding box.
Specs
clamp( vector :: Scenic.Math.vector_2(), min :: Scenic.Math.vector_2(), max :: Scenic.Math.vector_2() ) :: Scenic.Math.vector_2()
Clamp a vector to the space between two other vectors.
Parameters:
vector2
- the vector to be clampedmin
- the vector defining the minimum boundarymax
- the vector defining the maximum boundary
Returns: A vector derived from the space between two other vectors
Specs
cross(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: number()
Calculates the cross product of two vectors.
Parameters:
vector2_a
- the first vectorvector2_b
- the second vector
Returns: A number which is the result of the cross product
Specs
div(vector2 :: Scenic.Math.vector_2(), scalar :: number()) :: Scenic.Math.vector_2()
Divide a vector by a scalar.
Parameters:
vector2
- the vectorscalar
- the scalar value
Returns: A new vector which is the result of the division
Specs
dot(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: number()
Calculates the dot product of two vectors.
Parameters:
vector2_a
- the first vectorvector2_b
- the second vector
Returns: A number which is the result of the dot product
A vector that points straight down by 1.
Specs
in_bounds?(vector :: Scenic.Math.vector_2(), bounds :: Scenic.Math.vector_2()) :: boolean()
Determine if a vector is in the bounds (or clamp space) between two other vectors.
Parameters:
vector2
- the vector to be testedbounds
- a vector defining the boundary
Returns: true or false
Specs
in_bounds?( vector :: Scenic.Math.vector_2(), min :: Scenic.Math.vector_2(), max :: Scenic.Math.vector_2() ) :: boolean()
Determine if a vector is in the bounds (or clamp space) between two other vectors.
Parameters:
vector2
- the vector to be testedmin
- the vector defining the minimum boundarymax
- the vector defining the maximum boundary
Returns: A vector derived from the space between two other vectors
Specs
invert(vector_2 :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Invert a vector.
Parameters:
vector_2
- the vector to be inverted
Returns: The inverted vector
Examples
iex> Scenic.Math.Vector2.invert({2, 2})
{-2, -2}
A vector that points left by 1.
Specs
length(vector2 :: Scenic.Math.vector_2()) :: number()
Calculates the length of the vector.
This is slower than calculating the squared length.
Parameters:
vector2
- the vector
Returns: A number which is the length
Specs
length_squared(vector2 :: Scenic.Math.vector_2()) :: number()
Calculates the squared length of the vector.
This is faster than calculating the length if all you want to do is compare the lengths of two vectors against each other.
Parameters:
vector2
- the vector
Returns: A number which is the square of the length
Specs
lerp( vector_a :: Scenic.Math.vector_2(), vector_b :: Scenic.Math.vector_2(), t :: number() ) :: Scenic.Math.vector_2()
Calculate the lerp of two vectors.
See This explanation for more info.
Parameters:
vector_a
- the first vectorvector_b
- the second vectort
- the "t" value (see link above). Must be between 0 and 1.
Returns: A vector, which is the result of the lerp.
Specs
max(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Find a new vector derived from the highest x
and y
from two given vectors.
Parameters:
vector2_a
- the first vectorvector2_b
- the second vector
Returns:
A vector derived from the highest x
and y
from two given vectors
Specs
min(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Find a new vector derived from the lowest x
and y
from two given vectors.
Parameters:
vector2_a
- the first vectorvector2_b
- the second vector
Returns:
A vector derived from the lowest x
and y
from two given vectors
Specs
mul(vector2 :: Scenic.Math.vector_2(), scalar :: number()) :: Scenic.Math.vector_2()
Multiply a vector by a scalar.
Parameters:
vector2
- the vectorscalar
- the scalar value
Returns: A new vector which is the result of the multiplication
Specs
nlerp( vector_a :: Scenic.Math.vector_2(), vector_b :: Scenic.Math.vector_2(), t :: number() ) :: Scenic.Math.vector_2()
Calculate the nlerp (normalized lerp) of two vectors.
See This explanation for more info.
Parameters:
vector_a
- the first vectorvector_b
- the second vectort
- the "t" value (see link above). Must be between 0 and 1.
Returns: A vector, which is the result of the nlerp.
Specs
normalize(vector2 :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Normalize a vector so it has the same angle, but a length of 1.
Parameters:
vector2
- the vector
Returns: A vector with the same angle as the original, but a length of 1
A vector that points to {1,1}.
Specs
project( vector :: Scenic.Math.vector_2() | [Scenic.Math.vector_2()], matrix :: Scenic.Math.matrix() ) :: Scenic.Math.vector_2() | [Scenic.Math.vector_2()]
Project a vector into the space defined by a matrix
Parameters:
vector
- the vector, or a list of vectorsmatrix
- the matrix
Returns: A projected vector (or list of vectors)
A vector that points right by 1.
Specs
round(vector_2 :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Round the values of a vector to the nearest integers.
Parameters:
vector_2
- the vector to be rounded
Returns: The integer vector
Examples
iex> Scenic.Math.Vector2.round({1.2, 1.56})
{1, 2}
Specs
sub(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Subtract one vector from another.
Parameters:
vector2_a
- the first vectorvector2_b
- the second vector, which will be subtracted from the first
Returns: A new vector which is the result of the subtraction
Specs
trunc(vector_2 :: Scenic.Math.vector_2()) :: Scenic.Math.vector_2()
Truncate the values of a vector into integers.
Parameters:
vector_2
- the vector to be truncated
Returns: The integer vector
Examples
iex> Scenic.Math.Vector2.trunc({1.6, 1.2})
{1, 1}
A vector that points to {1,0}.
A vector that points to {0,1}.
A vector that points straight up by 1.
A vector that points to the origin.