Line of Sight tcod.los#

This modules holds functions for NumPy-based line of sight algorithms.

tcod.los.bresenham(start: tuple[int, int], end: tuple[int, int]) NDArray[np.intc][source]#

Return a thin Bresenham line as a NumPy array of shape (length, 2).

start and end are the endpoints of the line. The result always includes both endpoints, and will always contain at least one index.

You might want to use the results as is, convert them into a list with numpy.ndarray.tolist or transpose them and use that to index another 2D array.

Example:

>>> import tcod
>>> tcod.los.bresenham((3, 5),(7, 7)).tolist()  # Convert into list.
[[3, 5], [4, 5], [5, 6], [6, 6], [7, 7]]
>>> tcod.los.bresenham((0, 0), (0, 0))
array([[0, 0]]...)
>>> tcod.los.bresenham((0, 0), (4, 4))[1:-1]  # Clip both endpoints.
array([[1, 1],
       [2, 2],
       [3, 3]]...)

>>> array = np.zeros((5, 5), dtype=np.int8)
>>> array
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=int8)
>>> tcod.los.bresenham((0, 0), (3, 4)).T  # Transposed results.
array([[0, 1, 1, 2, 3],
       [0, 1, 2, 3, 4]]...)
>>> indexes_ij = tuple(tcod.los.bresenham((0, 0), (3, 4)).T)
>>> array[indexes_ij] = np.arange(len(indexes_ij[0]))
>>> array
array([[0, 0, 0, 0, 0],
       [0, 1, 2, 0, 0],
       [0, 0, 0, 3, 0],
       [0, 0, 0, 0, 4],
       [0, 0, 0, 0, 0]], dtype=int8)
>>> array[indexes_ij]
array([0, 1, 2, 3, 4], dtype=int8)

New in version 11.14.