diff --git a/lib/vector.ex b/lib/vector.ex new file mode 100644 index 0000000..e45ea29 --- /dev/null +++ b/lib/vector.ex @@ -0,0 +1,104 @@ +defmodule Vector do + @moduledoc """ + Describes a two dimensional vector that connects the origin to a point (x, y) + """ + + defstruct x: 0, y: 0 + + @doc """ + Hello world. + + ## Examples + + iex> Vector.hello + :world + + """ + def hello do + :world + end + + @doc """ + Adds two vectors. + + ## Examples + + iex> Vector.add(%Vector{x: 1, y: 1}, %Vector{x: 3, y: 4}) + %Vector{x: 4, y: 5} + + """ + def add(%{x: x1, y: y1}, %{x: x2, y: y2}) do + %Vector{x: x1 + x2, y: y1 + y2} + end + + @doc """ + Negates a vector. + + ## Examples + + iex> Vector.neg(%Vector{x: 1, y: 1}) + %Vector{x: -1, y: -1} + + """ + def neg(%{x: x, y: y}) do + %Vector{x: -x, y: -y} + end + + @doc """ + Subtracts two vectors. + + ## Examples + + iex> Vector.sub(%Vector{x: 1, y: 1}, %Vector{x: 3, y: 4}) + %Vector{x: -2, y: -3} + + """ + def sub(v1, v2) do + add(v1, neg(v2)) + end + + @doc """ + Scales a vector. + + ## Examples + + iex> Vector.scale(1.5, %Vector{x: 2, y: 3}) + %Vector{x: 3.0, y: 4.5} + + """ + def scale(ratio, %{x: x, y: y}) do + %Vector{x: ratio * x, y: ratio * y} + end + + @doc """ + Computes the length of a vector. + + ## Examples + + iex> Vector.length(%Vector{x: 3, y: 4}) + 5.0 + + """ + def length(%{x: x, y: y}) do + :math.sqrt(x * x + y * y) + end + + @doc """ + Converts a Vector to a string using a certain separator. + + ## Examples + + iex> Vector.to_string_with(%Vector{x: 3, y: 4}, ",") + "3,4" + + """ + def to_string_with(%{x: x, y: y}, separator) do + "#{x}#{separator}#{y}" + end + + defimpl String.Chars, for: Vector do + def to_string(vector) do + Vector.to_string_with(vector, ",") + end + end +end diff --git a/mix.exs b/mix.exs index 2acd1ab..8505d6e 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule ScenicEscher.MixProject do [ app: :scenic_escher, version: "0.1.0", - elixir: "~> 1.7", + elixir: "~> 1.8", build_embedded: true, start_permanent: Mix.env() == :prod, deps: deps() diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/test/vector_test.exs b/test/vector_test.exs new file mode 100644 index 0000000..d3e46dc --- /dev/null +++ b/test/vector_test.exs @@ -0,0 +1,4 @@ +defmodule VectorTest do + use ExUnit.Case + doctest Vector +end