Skip to content

Latest commit

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Bresenham

Bresenham's line algorithm written in Lua.

Overview

Arguments

The Bresenham.line function expects the following arguments:

  • ox (number) The origin's x-coordinates. The line will start here.
  • oy (number) The origin's y-coordinates. The line will start here.
  • tx (number) The target's x-coordinates. The line will end here.
  • ty (number) The target's y-coordinates. The line will end here.
  • callback (function) A callback function being called for every tile the line passes. The line algorithm will stop if the callback returns false (optional).
  • ... (varargs) Additional variables that should be passed to the callback function (optional).

Return values

  • (boolean) True if the line has reached its target, false if it stopped early.
  • (number) The number of tiles traversed by the line.

Usage

If you don't provide a callback function the line algorithm will always return true and can be used to count distances on the grid:

local_, counter=Bresenham.line( 1, 1, 7, 7 )

By providing a callback you can control how the line algorithm behaves on the grid. For example you could tell it to stop early if it hits certain tiles, objects, monsters and so on ...

localfunctioncallback( x, y, counter, ... )
-- Unpack the varargs passed after the callback.localvararg1, vararg2=...;
-- Check if the line algorithm should stop early.ifnotgrid[x][y]:isPassable() thenreturnfalseendreturntrueendBresenham.line( 1, 1, 10, 10, callback, 'foo', 'bar' )

Complete example:

localBresenham=require( 'Bresenham' )
localgrid= {
{ '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#' },
{ '#', '.', '.', '#', '#', '.', '.', '#' },
{ '#', '.', '.', '#', '#', '.', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#' }
}
print( 'Traverse grid if no obstacles are hit:' )
localsuccess, counter=Bresenham.line( 2, 2, 6, 2, function( x, y, counter )
print( string.format( 'x: %d, y: %d, steps: %d, tile: %s', x, y, counter, grid[x][y] ))
ifgrid[x][y] =='#' thenreturnfalseendreturntrueend)
print( string.format( 'Reached target: %s after %d steps.\n', tostring( success ), counter ))
print( 'Stop line early if obstacles are hit:' )
localsuccess, counter=Bresenham.line( 2, 2, 6, 6, function( x, y, counter )
print( string.format( 'x: %d, y: %d, steps: %d, tile: %s', x, y, counter, grid[x][y] ))
ifgrid[x][y] =='#' thenreturnfalseendreturntrueend)
print( string.format( 'Reached target: %s after %d steps.\n', tostring( success ), counter ))
print( 'Without a callback just count the steps from start to finish:' )
local_, counter=Bresenham.line( 1, 1, 7, 7 )
print( counter )

About

Bresenham's line algorithm written in Lua.

Topics

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages