Scenic.Math.Vector2 (Scenic v0.10.3) 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.

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

Link to this function

add(vector2_a, vector2_b)

View Source

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 added
  • vector2_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

Clamp a vector to the space between two other vectors.

Parameters:

  • vector2 - the vector to be clamped
  • min - the vector defining the minimum boundary
  • max - the vector defining the maximum boundary

Returns: A vector derived from the space between two other vectors

Link to this function

cross(vector2_a, vector2_b)

View Source

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 vector
  • vector2_b - the second vector

Returns: A number which is the result of the cross product

Link to this function

distance(vector2_a, vector2_b)

View Source
Link to this function

div(vector2_a, vector2_b)

View Source

Specs

div(vector2 :: Scenic.Math.vector_2(), scalar :: number()) ::
  Scenic.Math.vector_2()

Divide a vector by a scalar.

Parameters:

  • vector2 - the vector
  • scalar - the scalar value

Returns: A new vector which is the result of the division

Link to this function

dot(vector2_a, vector2_b)

View Source

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 vector
  • vector2_b - the second vector

Returns: A number which is the result of the dot product

A vector that points straight down by 1.

Link to this function

in_bounds?(vector, bounds)

View Source

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 tested
  • bounds - a vector defining the boundary

Returns: true or false

Link to this function

in_bounds?(vector, min, max)

View Source

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 tested
  • min - the vector defining the minimum boundary
  • max - 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

Link to this function

lerp(vector_a, vector_a, t)

View Source

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 vector
  • vector_b - the second vector
  • t - the "t" value (see link above). Must be between 0 and 1.

Returns: A vector, which is the result of the lerp.

Link to this function

max(vector2_a, vector2_b)

View Source

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 vector
  • vector2_b - the second vector

Returns: A vector derived from the highest x and y from two given vectors

Link to this function

min(vector2_a, vector2_b)

View Source

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 vector
  • vector2_b - the second vector

Returns: A vector derived from the lowest x and y from two given vectors

Link to this function

mul(vector2_a, vector2_b)

View Source

Specs

mul(vector2 :: Scenic.Math.vector_2(), scalar :: number()) ::
  Scenic.Math.vector_2()

Multiply a vector by a scalar.

Parameters:

  • vector2 - the vector
  • scalar - the scalar value

Returns: A new vector which is the result of the multiplication

Link to this function

nlerp(vector_a, vector_a, t)

View Source

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 vector
  • vector_b - the second vector
  • t - 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}.

Link to this function

project(vector_a, matrix)

View Source

Specs

Project a vector into the space defined by a matrix

Parameters:

  • vector - the vector, or a list of vectors
  • matrix - 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}
Link to this function

sub(vector2_a, vector2_b)

View Source

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 vector
  • vector2_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.