Skip to content

Commit

Permalink
Merge pull request #3 from pappersverk/issue-2-vertical-lines
Browse files Browse the repository at this point in the history
Fix aritmetic error on vertical lines using line/5
  • Loading branch information
luisgabrielroldan authored Mar 4, 2020
2 parents f065581 + 5cda2f7 commit 62a6782
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
25 changes: 19 additions & 6 deletions lib/oled/display/impl/ssd_1306/draw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ defmodule OLED.Display.Impl.SSD1306.Draw do
{x1, y1, x2, y2}
end

dx = x2 - x1
dy = y2 - y1
cond do
x1 == x2 ->
line_v(state, x1, y1, y2 - y1 + 1, opts)

Enum.reduce(x1..x2, state, fn x, acc ->
y = trunc(y1 + dy * (x - x1) / dx)
put_pixel(acc, x, y, opts)
end)
y1 == y2 ->
line_h(state, x1, y1, x2 - x1 + 1, opts)

true ->
do_line(state, x1, y1, x2, y2, opts)
end
end

def line_h(state, _x, _y, width, _opts) when width < 1,
Expand Down Expand Up @@ -132,4 +135,14 @@ defmodule OLED.Display.Impl.SSD1306.Draw do

defp draw_circle(_x0, _y0, _x, _y, _f, _ddF_x, _ddF_y, _opts, state),
do: state

defp do_line(state, x1, y1, x2, y2, opts) do
dx = x2 - x1
dy = y2 - y1

Enum.reduce(x1..x2, state, fn x, acc ->
y = trunc(y1 + dy * (x - x1) / dx)
put_pixel(acc, x, y, opts)
end)
end
end
33 changes: 32 additions & 1 deletion test/oled/display/impl/ssd_1306/line_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ defmodule OLED.Display.Impl.SSD1306.LineTest do
]
end

#|> Enum.each(&IO.inspect/1)
test "draw xor" do
assert build_state(@w, @h)
|> Draw.line(0, 0, 32, 8, [])
Expand Down Expand Up @@ -71,6 +70,38 @@ defmodule OLED.Display.Impl.SSD1306.LineTest do
]
end

test "draw horizontal rect" do
assert build_state(@w, @h)
|> Draw.line(4, 5, 16, 5, [])
|> ascii_render() ==
[
" ",
" ",
" ",
" ",
" ",
" ############# ",
" ",
" "
]
end

test "draw vertical rect" do
assert build_state(@w, @h)
|> Draw.line(10, 1, 10, 6, [])
|> ascii_render() ==
[
" ",
" # ",
" # ",
" # ",
" # ",
" # ",
" # ",
" "
]
end

test "draw rect out 2" do
assert build_state(@w, @h)
|> Draw.line(-32, -8, 32, 8, [])
Expand Down

0 comments on commit 62a6782

Please sign in to comment.