Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Created December 29, 2014 04:03
Show Gist options
  • Save patrickgombert/85cae911122d7f8b2acb to your computer and use it in GitHub Desktop.
Save patrickgombert/85cae911122d7f8b2acb to your computer and use it in GitHub Desktop.
def coords({x1, y1}, {x2, y2}) when x1 == x2 or y1 == y2 or abs(x1 - x2) == abs(y1 - y2) do
do_coords({x1, y1}, {x2, y2}, [])
end
def coords(_, _), do: raise "invalid"
defp do_coords({fromx, fromy}, {tox, toy}, acc) when fromx == tox and fromy == toy, do: tl(acc)
defp do_coords({fromx, fromy}, {tox, toy}, acc) do
newx = gen_next(fromx, tox)
newy = gen_next(fromy, toy)
do_coords({newx, newy}, {tox, toy}, acc ++ [{fromx, fromy}])
end
defp gen_next(x, y) do
cond do
x < y -> x + 1
x > y -> x - 1
true -> x
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment