Created
December 29, 2014 04:03
-
-
Save patrickgombert/85cae911122d7f8b2acb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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