stella v0.6.0 Vector2D

Documentation for Vector 2D.

Link to this section Summary

Functions

Increment one vector by another one

Calculate distance between vectors

Divide vector by another one

Check if given vectors are equal

Returns vector length

Multiply vector by another one

Create a new two dimensional vector from given values

Rotate vector by angel

Scale vector by given scalar

Decrement one vector by another one

Convert vector from struct to list

Convert vector from list to struct

Link to this section Functions

Link to this function

add(curr_vector, given_vector)

Specs

add(%{x: number(), y: number()}, %{x: number(), y: number()}) :: %{
  x: number(),
  y: number()
}

Increment one vector by another one

Examples

iex> Vector2D.add(%{x: 2, y: 2}, %{x: 2, y: 2})
%{x: 4, y: 4}

iex> Vector2D.new(-3, 3) |> Vector2D.add(%{x: 3, y: 6})
%{x: 0, y: 9}
Link to this function

distance(curr_vector, given_vector)

Specs

distance(%{x: number(), y: number()}, %{x: number(), y: number()}) :: float()

Calculate distance between vectors

Examples

iex> Vector2D.distance(%{x: 2.0, y: 2.0}, %{x: 2, y: 2.0})
0.0

iex> Vector2D.distance(%{x: -2, y: 4}, %{x: 2, y: 2.0})
4.47213595499958
Link to this function

divide(curr_vector, given_vector)

Specs

divide(%{x: number(), y: number()}, %{x: number(), y: number()}) :: %{
  x: number(),
  y: number()
}

Divide vector by another one

Examples

iex> Vector2D.divide(%{x: 2, y: 2}, %{x: 2, y: 2})
%{x: 1.0, y: 1.0}

iex> Vector2D.new(-3, 3) |> Vector2D.divide(%{x: 2, y: 3})
%{x: -1.5, y: 1.0}
Link to this function

equals(curr_vector, given_vector)

Specs

equals(%{x: number(), y: number()}, %{x: number(), y: number()}) :: boolean()

Check if given vectors are equal

Examples

iex> Vector2D.equals(%{x: 2, y: 2}, %{x: 2, y: 2})
true

iex> Vector2D.equals(%{x: 2, y: 2}, %{x: 2.0, y: 2.0})
true

iex> Vector2D.equals(%{x: 2.0, y: 2.0}, %{x: 2, y: 2})
true

iex> Vector2D.equals(%{x: 2.0, y: 2.0}, %{x: 2, y: 2.0})
true

iex> Vector2D.new(-3, 3) |> Vector2D.equals(%{x: 2, y: 3})
false

Specs

length(%{x: number(), y: number()}) :: float()

Returns vector length

Two dimensional vector length chart

print image

Examples

iex> Vector2D.length(%{x: 2, y: 2})
2.8284271247461903

iex> Vector2D.new(-3, 3) |> Vector2D.length()
4.242640687119285
Link to this function

multiply(curr_vector, given_vector)

Specs

multiply(%{x: number(), y: number()}, %{x: number(), y: number()}) :: %{
  x: number(),
  y: number()
}

Multiply vector by another one

Examples

iex> Vector2D.multiply(%{x: 2, y: 2}, %{x: 2, y: 2})
%{x: 4, y: 4}

iex> Vector2D.new(-3, 3) |> Vector2D.multiply(%{x: 2, y: 3})
%{x: -6, y: 9}
Link to this function

new(x \\ 0, y \\ 0)

Specs

new(number(), number()) :: %{x: number(), y: number()}

Create a new two dimensional vector from given values

Examples

iex> Vector2D.new(10, 1)
%{x: 10, y: 1}

iex> Vector2D.new()
%{x: 0, y: 0}
Link to this function

rotate(vector, angle)

Specs

rotate(%{x: number(), y: number()}, number()) :: %{x: number(), y: number()}

Rotate vector by angel

Examples

iex> Vector2D.rotate(%{x: 2.0, y: 2.0}, 2)
%{x: -2.650888526745648, y: 0.9863011805570786}
Link to this function

scale(vector, scalar)

Specs

scale(%{x: number(), y: number()}, number()) :: %{x: number(), y: number()}

Scale vector by given scalar

Examples

iex> Vector2D.scale(%{x: 2, y: 2}, 2)
%{x: 4, y: 4}

iex> Vector2D.new(-3, 3) |> Vector2D.scale(3)
%{x: -9, y: 9}

iex> Vector2D.new(-3, 3.12) |> Vector2D.scale(3.001)
%{x: -9.003, y: 9.36312}
Link to this function

sub(curr_vector, given_vector)

Specs

sub(%{x: number(), y: number()}, %{x: number(), y: number()}) :: %{
  x: number(),
  y: number()
}

Decrement one vector by another one

Examples

iex> Vector2D.sub(%{x: 2, y: 2}, %{x: 2, y: 2})
%{x: 0, y: 0}

iex> Vector2D.new(-3, 3) |> Vector2D.sub(%{x: 3, y: 6})
%{x: -6, y: -3}
Link to this function

to_list(vector)

Specs

to_list(%{x: number(), y: number()}) :: [...]
to_list([...]) :: %{x: number(), y: number()}

Convert vector from struct to list

Examples

iex> Vector2D.to_list(%{x: 1, y: 2})
[1, 2]
Link to this function

to_struct(vector)

Convert vector from list to struct

Examples

iex> Vector2D.to_struct([1, 2])
%{x: 1, y: 2}