Scenic.Math.Matrix (Scenic v0.10.3) View Source

A collection of functions to work with matrices.

All the matrix fucntions in this module work exclusively with the binary form of a matrix, which is a compact binary of 16 4-byte floats.

If you would like to convert back and forth from the more human friendly list version, then please use the functions in Scenic.Math.Matrix.Utils

Link to this section Summary

Functions

Add two matrices together.

Calculate the adjugate of a matrix

Build a matrix that represents a 2D rotation around a point.

Build a matrix that represents a 2D rotation around the origin.

Build a matrix that represents a scaling operation.

Build a matrix that represents a simple translation.

Test if two matrices are close in value to each other.

Calculate the determinant of a matrix

Divide a matrix by a scalar.

Get a single value out of a binary matrix.

Extract the 2D vector represented by the matrix.

The identity matrix

Inverte a matrix.

Multiply a list of matrices together.

Multiply a matrix by another matrix or a scalar.

Project a vector by a matrix.

Project a list of vectors by a matrix.

Put a single value into a binary matrix.

Multiply a matrix by a rotation.

Multiply a matrix by a scale factor.

Subtract one matrix from another.

Multiply a matrix by a translation.

Transpose a matrix

A matrix where all the values are 0

Link to this section Functions

Specs

add(matrix_a :: Scenic.Math.matrix(), matrix_b :: Scenic.Math.matrix()) ::
  Scenic.Math.matrix()

Add two matrices together.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix_a: The first matrix
  • matrix_b: The second matrix

Returns: The resulting matrix

Specs

adjugate(matrix :: Scenic.Math.matrix()) :: Scenic.Math.matrix()

Calculate the adjugate of a matrix

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix

Returns: The resulting matrix

Link to this function

build_rotate_around(angle, pin)

View Source

Specs

build_rotate_around(angle :: number(), pin :: Scenic.Math.point()) ::
  Scenic.Math.matrix()

Build a matrix that represents a 2D rotation around a point.

Parameters:

  • angle: the amount to rotate, in radians
  • pin: position to pin the rotation around

Returns: A binary matrix

Specs

build_rotation(angle :: number()) :: Scenic.Math.matrix()

Build a matrix that represents a 2D rotation around the origin.

Parameters:

  • angle: the amount to rotate, in radians

Returns: A binary matrix

Specs

build_scale(scale :: number() | Scenic.Math.vector_2()) :: Scenic.Math.matrix()

Build a matrix that represents a scaling operation.

Parameters:

  • scale: the amount to scale by. Can be either a number or a vector_2

Returns: A binary matrix

Link to this function

build_translation(vector_2)

View Source

Specs

build_translation(vector_2 :: Scenic.Math.vector_2()) :: Scenic.Math.matrix()

Build a matrix that represents a simple translation.

Parameters:

  • vector_2: the vector defining how much to translate

Returns: A binary matrix

Link to this function

close?(matrix_a, matrix_b, tolerance \\ 1.0e-6)

View Source

Specs

close?(
  matrix_a :: Scenic.Math.matrix(),
  matrix_a :: Scenic.Math.matrix(),
  tolerance :: number()
) :: boolean()

Test if two matrices are close in value to each other.

Parameters:

  • matrix_a: The first matrix
  • matrix_b: The second matrix
  • tolerance: Defines what close means. Defaults to: 0.000001

Returns: A boolean

Specs

determinant(matrix :: Scenic.Math.matrix()) :: Scenic.Math.matrix()

Calculate the determinant of a matrix

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix

Returns: The resulting matrix

Specs

div(matrix :: Scenic.Math.matrix(), divisor :: number()) :: Scenic.Math.matrix()

Divide a matrix by a scalar.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix
  • divisor: A number (scalar) to divide by

Returns: The resulting matrix

Specs

get(matrix :: Scenic.Math.matrix(), x :: number(), y :: number()) :: number()

Get a single value out of a binary matrix.

Parameters:

  • matrix: The source matrix
  • x: the column to pull the data from
  • y: the row to pull the data from

Returns: A number

Specs

get_xy(matrix :: Scenic.Math.matrix()) :: Scenic.Math.vector_2()

Extract the 2D vector represented by the matrix.

Parameters:

  • matrix: The source matrix

Returns: A vector_2

Specs

identity() :: Scenic.Math.matrix()

The identity matrix

Specs

invert(matrix :: Scenic.Math.matrix()) :: Scenic.Math.matrix()

Inverte a matrix.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix

Returns: The resulting matrix

Specs

mul(matrix_list :: [Scenic.Math.matrix()]) :: Scenic.Math.matrix()

Multiply a list of matrices together.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix_list: A list of matrices

Returns: The resulting matrix

Specs

mul(
  matrix :: Scenic.Math.matrix(),
  multiplier :: number() | Scenic.Math.matrix()
) :: Scenic.Math.matrix()

Multiply a matrix by another matrix or a scalar.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix
  • multiplier: A number (scalar) or a matrix to multiply by

Returns: The resulting matrix

Link to this function

project_vector(matrix, arg)

View Source

Specs

project_vector(
  matrix :: Scenic.Math.matrix(),
  vector_2 :: Scenic.Math.vector_2()
) :: Scenic.Math.vector_2()
project_vector(
  matrix :: Scenic.Math.matrix(),
  vector_list :: [Scenic.Math.vector_2()]
) :: [Scenic.Math.vector_2()]

Project a vector by a matrix.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix
  • vector: The vector to project

Returns: The projected vector

Link to this function

project_vectors(a, vector_bin)

View Source

Project a list of vectors by a matrix.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix
  • vectors: The list of vectors to project

Returns: A list of projected vectors

Specs

put(matrix :: Scenic.Math.matrix(), x :: number(), y :: number(), v :: number()) ::
  Scenic.Math.matrix()

Put a single value into a binary matrix.

Parameters:

  • matrix: The source matrix
  • x: the column to pull the data from
  • y: the row to pull the data from
  • v: the value to put into the matrix. Must be a number.

Returns: A number

Specs

rotate(matrix :: Scenic.Math.matrix(), angle :: number() | nil) ::
  Scenic.Math.matrix()

Multiply a matrix by a rotation.

Parameters:

  • matrix: The incoming source matrix
  • angle: the amount to rotate, in radians or nil (which does nothing

Returns: A binary matrix

Specs

scale(
  matrix :: Scenic.Math.matrix(),
  scale :: number() | Scenic.Math.vector_2() | nil
) :: Scenic.Math.matrix()

Multiply a matrix by a scale factor.

Parameters:

  • matrix: The incoming source matrix
  • scale: the amount to scale by. Can be either a number, a vector, or nil (which does nothing)

Returns: A binary matrix

Specs

sub(matrix_a :: Scenic.Math.matrix(), matrix_b :: Scenic.Math.matrix()) ::
  Scenic.Math.matrix()

Subtract one matrix from another.

This operation is implemented as a NIF for performance.

Parameters:

  • matrix_a: The first matrix
  • matrix_b: The second matrix, which is subtracted from the first

Returns: The resulting matrix

Link to this function

translate(matrix, vector_2)

View Source

Specs

translate(
  matrix :: Scenic.Math.matrix(),
  vector_2 :: Scenic.Math.vector_2() | nil
) :: Scenic.Math.matrix()

Multiply a matrix by a translation.

Parameters:

  • matrix: The incoming source matrix
  • vector_2: the vector to translate by or nil (which does nothing)

Returns: A binary matrix

Specs

transpose(matrix :: Scenic.Math.matrix()) :: Scenic.Math.matrix()

Transpose a matrix

This operation is implemented as a NIF for performance.

Parameters:

  • matrix: A matrix

Returns: The resulting matrix

Specs

zero() :: Scenic.Math.matrix()

A matrix where all the values are 0