Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Created December 19, 2016 08:48
Show Gist options
  • Select an option

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

Select an option

Save Chadtech/5e082cf909c12162f0ec5c5ffa90cf82 to your computer and use it in GitHub Desktop.
type BrensemhamStatics = Statics (Int, Int) (Float, Float) Coordinate
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) / 2
bresenhamLineRecursion =
Statics (sx, sy) (dx, dy) (x1, y1)
|>bresenhamLineLoop
in
bresenhamLineRecursion error (x0, y0) []
bresenhamLineLoop : BrensemhamStatics -> Float -> Coordinate -> List Coordinate -> List Coordinate
bresenhamLineLoop statics error (x0, y0) coordinates =
case statics of
Statics (sx, sy) (dx, dy) (x1, y1) ->
let coordinates_ = (x0, y0) :: coordinates in
if (x0 == x1) && (y0 == y1) then
coordinates_
else
let
(dErrX, x0_) =
if error > -dx then (-dy, sx + x0)
else (0, x0)
(dErrY, y0_) =
if error < dy then (dx, sy + y0)
else (0, y0)
error_ = error + dErrX + dErrY
in
bresenhamLineLoop statics error_ (x0_, y0_) coordinates_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment