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 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}
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
Calculate the magnitude of a vector.
Examples
iex> Vector.magnitude(%Collision.Vector2{x: 3.0, y: 4.0})
5.0
Calculate the squared magnitude of a vector.
Examples
iex> Vector.magnitude(%Collision.Vector2{x: 3.0, y: 4.0})
25.0
Normalize a vector.
Examples
iex> Vector.normalize(%Collision.Vector2{x: 3.0, y: 4.0})
%Collision.Vector2{x: 0.6, y: 0.8}
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 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}
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 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}