# 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

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

View Source

## Specs

add(vector2_a :: Scenic.Math.vector_2(), vector2_b :: Scenic.Math.vector_2()) ::
Scenic.Math.vector_2()

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

{4.0, 8.0}

# clamp(vector, min, max)

View Source

## 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

# 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

View Source

View Source

# 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

# 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

# down()

View Source

A vector that points straight down by 1.

# 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

# 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

# invert(vector_2)

View Source

## 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}

# left()

View Source

A vector that points left by 1.

# length(vector2)

View Source

## 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

# length_squared(vector2)

View Source

## 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

# 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.

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.

# 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

# 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

# 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

# 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.

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.

# normalize(vector2)

View Source

## 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

# one()

View Source

A vector that points to {1,1}.

# 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)

# right()

View Source

A vector that points right by 1.

# round(vector_2)

View Source

## 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}

# 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

# trunc(vector_2)

View Source

## 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}

# unity_x()

View Source

A vector that points to {1,0}.

# unity_y()

View Source

A vector that points to {0,1}.

# up()

View Source

A vector that points straight up by 1.