View Source Scenic.Math.Vector2 (Scenic v0.11.2)

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

Link to this function

add(vector2_a, vector2_b)

View Source
@spec 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

Examples

iex> Scenic.Math.Vector2.add({1.0, 5.0}, {3.0, 3.0})
{4.0, 8.0}
@spec 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.

@spec 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 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
@spec 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
@spec 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
@spec 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
@spec 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
@spec 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

@spec 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

Examples

iex> Scenic.Math.Vector2.invert({2, 2})
{-2, -2}

A vector that points left by 1.

@spec 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

@spec 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
@spec 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
@spec 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
@spec 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
@spec 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
@spec 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.

@spec 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

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.

@spec 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

Examples

iex> Scenic.Math.Vector2.round({1.2, 1.56})
{1, 2}
Link to this function

sub(vector2_a, vector2_b)

View Source
@spec 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

@spec 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

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.