Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Created December 19, 2016 07:51
Show Gist options
  • Select an option

  • Save Chadtech/a44666a0f36d22e4d8b544e772d41948 to your computer and use it in GitHub Desktop.

Select an option

Save Chadtech/a44666a0f36d22e4d8b544e772d41948 to your computer and use it in GitHub Desktop.
bresenhamLine : Coordinate -> Coordinate -> List Coordinate
bresenhamLine (x0, y0) (x1, y1) =
let
dx = (toFloat << abs) (x1 - x0)
sx = if x0 < x1 then 1 else -1
dy = (toFloat << abs) (y1 - y0)
sy = if y0 < y1 then 1 else -1
error = if dx > dy then dx else -dy
in
bresenhamLineLoop
(sx, sy)
(dx, dy)
(x1, y1)
(error / 2)
(x0, y0)
[]
bresenhamLineLoop : (Int, Int) -> (Float, Float) -> Coordinate -> Float -> Coordinate -> List Coordinate -> List Coordinate
bresenhamLineLoop (sx, sy) (dx, dy) (x1, y1) error (x0, y0) coordinates =
let
coordinates_ = (x0, y0) :: coordinates
in
if (x0 == x1) && (y0 == y1) then coordinates_
else
let
(dErrorX, dx0_) =
if error > -dx then (-dy, sx)
else (0, 0)
(dErrorY, dy0_) =
if error < dy then (dx, sy)
else (0, 0)
in
bresenhamLineLoop
(sx, sy)
(dx, dy)
(x1, y1)
(error + dErrorX + dErrorY)
(x0 + dx0_, y0 + dy0_)
coordinates_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment