Compare commits

...

2 Commits

Author SHA1 Message Date
blueloveTH
034897f92d fix docs 2025-02-21 14:54:18 +08:00
blueloveTH
dc8df160d6 add vec2i.__floordiv__ 2025-02-21 11:38:11 +08:00
4 changed files with 29 additions and 8 deletions

View File

@ -25,7 +25,7 @@ class array2d_like[T]:
def get[R](self, col: int, row: int, default: R = None) -> T | R:
"""Get the value at the given position.
If the position is out of bounds, return the default value.
If the position is out of bounds, returns the default value.
"""
def render(self) -> str: ...
@ -100,7 +100,7 @@ class array2d_like[T]:
"""Convolve the array with the given kernel."""
def get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:
"""Get connected components of the grid.
"""Get connected components of the grid via BFS algorithm.
Returns the `visited` array and the number of connected components,
where `0` means unvisited, and non-zero means the index of the connected component.
@ -144,7 +144,7 @@ class chunked_array2d[T, TContext]:
def clear(self) -> None: ...
def world_to_chunk(self, world_pos: vec2i) -> tuple[vec2i, vec2i]:
"""Convert world position to chunk position and local position."""
"""Converts world position to chunk position and local position."""
def add_chunk(self, chunk_pos: vec2i) -> TContext: ...
def remove_chunk(self, chunk_pos: vec2i) -> bool: ...

View File

@ -30,6 +30,7 @@ class _vecI[T]:
def __mul__(self, other: int) -> T: ...
@overload
def __mul__(self, other: T) -> T: ...
def __floordiv__(self, other: int) -> T: ...
def __hash__(self) -> int: ...
@ -59,21 +60,26 @@ class vec2(_vecF['vec2']):
@overload
def __init__(self, xy: vec2i) -> None: ...
def rotate(self, radians: float) -> vec2: ...
def rotate(self, radians: float) -> vec2:
"""Rotate the vector by `radians`.
+ If y axis is top to bottom, positive value means clockwise (default)
+ If y axis is bottom to top, positive value means counter-clockwise
"""
@staticmethod
def angle(__from: vec2, __to: vec2) -> float:
"""Returns the angle in radians between vectors `from` and `to`.
"""Return the angle in radians between vectors `from` and `to`.
The result range is `[-pi, pi]`.
+ if y axis is top to bottom, positive value means clockwise
+ if y axis is bottom to top, positive value means counter-clockwise
+ If y axis is top to bottom, positive value means clockwise (default)
+ If y axis is bottom to top, positive value means counter-clockwise
"""
@staticmethod
def smooth_damp(current: vec2, target: vec2, current_velocity: vec2, smooth_time: float, max_speed: float, delta_time: float) -> tuple[vec2, vec2]:
"""Smoothly changes a vector towards a desired goal over time.
"""Smoothly change a vector towards a desired goal over time.
Returns a new value that is closer to the target and current velocity.
"""

View File

@ -298,6 +298,16 @@ DEF_VECTOR_OPS(3)
sum += a.data[i] * b.data[i]; \
py_newint(py_retval(), sum); \
return true; \
} \
static bool vec##D##i##__floordiv__(int argc, py_Ref argv) { \
PY_CHECK_ARGC(2); \
PY_CHECK_ARG_TYPE(1, tp_int); \
c11_vec##D##i a = py_tovec##D##i(&argv[0]); \
py_i64 b = py_toint(&argv[1]); \
for(int i = 0; i < D; i++) \
a.data[i] /= b; \
py_newvec##D##i(py_retval(), a); \
return true; \
}
DEF_VECTOR_INT_OPS(2)
@ -911,6 +921,7 @@ void pk__add_module_linalg() {
py_bindmagic(vec2i, __add__, vec2i__add__);
py_bindmagic(vec2i, __sub__, vec2i__sub__);
py_bindmagic(vec2i, __mul__, vec2i__mul__);
py_bindmagic(vec2i, __floordiv__, vec2i__floordiv__);
py_bindmagic(vec2i, __eq__, vec2i__eq__);
py_bindmagic(vec2i, __ne__, vec2i__ne__);
py_bindmagic(vec2i, __hash__, vec2i__hash__);
@ -935,6 +946,7 @@ void pk__add_module_linalg() {
py_bindmagic(vec3i, __add__, vec3i__add__);
py_bindmagic(vec3i, __sub__, vec3i__sub__);
py_bindmagic(vec3i, __mul__, vec3i__mul__);
py_bindmagic(vec3i, __floordiv__, vec3i__floordiv__);
py_bindmagic(vec3i, __eq__, vec3i__eq__);
py_bindmagic(vec3i, __ne__, vec3i__ne__);
py_bindmagic(vec3i, __hash__, vec3i__hash__);

View File

@ -388,6 +388,9 @@ assert vec3i(1, 2, 3) * vec3i(4, 5, 6) == vec3i(4, 10, 18)
assert vec3i(1, 2, 3) * 2 == vec3i(2, 4, 6)
assert vec3i(1, 2, 3).dot(vec3i(4, 5, 6)) == 32
assert vec2i(3, 5) // 2 == vec2i(1, 2)
assert vec3i(3, 5, 8) // 2 == vec3i(1, 2, 4)
a = {}
a[vec2i(1, 2)] = 1
assert a[vec2i(1, 2)] == 1