collision v0.3.1 Vector protocol

Summary

Functions

Add two vectors together

Calculate the dot product of two vectors

Calculate the magnitude of a vector

Calculate the squared magnitude of a vector

Normalize a vector

Project a vector, v1, onto another, v2

Round all the vector components to n decimal places

Multiple a vector by a scalar value

Subtract two vectors

Convert a vector to a tuple

Types

Functions

add(vector, vector)

Add two vectors together.

Examples

iex> Vector.add(%Collision.Vector2{x: 1.0, y: 1.0}, %Collision.Vector2{x: 2.0, y: 2.0})
%Collision.Vector2{x: 3.0, y: 3.0}
dot_product(vector, vector)

Calculate the dot product of two vectors.

A negative value indicates they are moving away from each other, positive towards.

Examples

iex> Vector.dot_product(%Collision.Vector2{x: 3.0, y: 4.0}, %Collision.Vector2{x: -1.0, y: 2.0})
5.0
magnitude(vector)

Calculate the magnitude of a vector.

Examples

iex> Vector.magnitude(%Collision.Vector2{x: 3.0, y: 4.0})
5.0
magnitude_squared(vector)

Calculate the squared magnitude of a vector.

Examples

iex> Vector.magnitude(%Collision.Vector2{x: 3.0, y: 4.0})
25.0
normalize(vector)

Normalize a vector.

Examples

iex> Vector.normalize(%Collision.Vector2{x: 3.0, y: 4.0})
%Collision.Vector2{x: 0.6, y: 0.8}
projection(vector, vector)

Project a vector, v1, onto another, v2.

Examples

iex> Vector.projection(%Collision.Vector2{x: 3.0, y: 4.0}, %Collision.Vector2{x: -1.0, y: 2.0})
%Collision.Vector2{x: -2.23606797749979, y: 4.47213595499958}
round_components(vector, integer)

Round all the vector components to n decimal places.

Examples

iex> Vector.round_components(%Vector2{x: 1.32342, y: 4.23231}, 2)
%Vector2{x: 1.32, y: 4.23}
scalar_mult(vector, scalar)

Multiple a vector by a scalar value.

Examples

iex> Vector.scalar_mult(%Collision.Vector2{x: 5.0, y: 2.0}, -1)
%Collision.Vector2{x: -5.0, y: -2.0}
subtract(vector, vector)

Subtract two vectors.

Examples

iex> Vector.subtract(%Collision.Vector2{x: 4.0, y: 1.0}, %Collision.Vector2{x: 1.0, y: 4.0})
%Collision.Vector2{x: 3.0, y: -3.0}
to_tuple(vector)

Convert a vector to a tuple.

Examples

iex> Vector.to_tuple(%Collision.Vector2{x: 1.0, y: 1.5})
{1.0, 1.5}