-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathlos.py
More file actions
63 lines (52 loc) · 2.13 KB
/
los.py
File metadata and controls
63 lines (52 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""This modules holds functions for NumPy-based line of sight algorithms."""
from __future__ import annotations
from typing import Any
import numpy as np
from numpy.typing import NDArray
from tcod.loader import ffi, lib
def bresenham(start: tuple[int, int], end: tuple[int, int]) -> NDArray[np.intc]:
"""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
:any:`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)
.. versionadded:: 11.14
"""
x1, y1 = start
x2, y2 = end
length = lib.bresenham(x1, y1, x2, y2, 0, ffi.NULL)
array: np.ndarray[Any, np.dtype[np.intc]] = np.ndarray((length, 2), dtype=np.intc)
lib.bresenham(x1, y1, x2, y2, length, ffi.from_buffer("int*", array))
return array