batgrl.geometry.basic#

Basic geometry functions and types.

Functions

clamp(value, min, max)

If value is less than min, returns min; else if max is less than value, returns max; else returns value.

lerp(a, b, p)

Linear interpolation of a to b with proportion p.

points_on_circle(n[, radius, center, offset])

Return n points on a circle.

rect_slice(pos, size)

Return slices for indexing a rect in a numpy array.

round_down(n)

Like the built-in round, but always rounds down.

Classes

Point(y, x)

A 2-d point.

Size(height, width)

A rectangular area.

class batgrl.geometry.basic.Point(y: int, x: int)#

Bases: NamedTuple

A 2-d point.

Note that y-coordinate is before x-coordinate. This convention is used so that the 2-d arrays that underly a gadget’s data can be directly indexed with the point.

Parameters or attributes type-hinted Point can often take tuple[int, int] for convenience.

Parameters:
yint

y-coordinate of point.

xint

x-coordinate of point.

Attributes:
yint

y-coordinate of point.

xint

x-coordinate of point.

x: int#

x-coordinate of point.

y: int#

y-coordinate of point.

class batgrl.geometry.basic.Size(height: int, width: int)#

Bases: NamedTuple

A rectangular area.

Parameters or attributes type-hinted Size can often take tuple[int, int] for convenience.

Parameters:
heightint

Height of area.

widthint

Width of area.

Attributes:
heightint

Height of area.

widthint

Width of area.

rowsint

Alias for height.

columnsint

Alias for width.

centerPoint

Center of area.

property center: Point#

Center of area.

property columns: int#

Alias for width.

height: int#

Height of area.

property rows: int#

Alias for height.

width: int#

Width of area.

batgrl.geometry.basic.clamp(value: Real, min: Real | None, max: Real | None) Real#

If value is less than min, returns min; else if max is less than value, returns max; else returns value. A one-sided clamp is possible by setting min or max to None.

Parameters:
valueReal

Value to clamp.

minReal | None

Minimum of clamped value.

maxReal | None

Maximum of clamped value.

Returns:
Real

A value between min and max, inclusive.

batgrl.geometry.basic.lerp(a: Real, b: Real, p: Real) Real#

Linear interpolation of a to b with proportion p.

batgrl.geometry.basic.points_on_circle(n: int, radius: float = 1.0, center: tuple[float, float] = (0.0, 0.0), offset: float = 0.0) ndarray[Any, dtype[float32]]#

Return n points on a circle.

Parameters:
nint

Number of points on a circle.

radiusfloat, default: 1.0

Radius of circle.

centertuple[float, float], default: (0.0, 0.0)

Center of circle.

offsetfloat, default: 0.0

Rotate output points by offset radians.

Returns:
NDArray[np.float32]

An (n, 2)-shaped NDArray of points on a circle.

batgrl.geometry.basic.rect_slice(pos: Point, size: Size) tuple[slice, slice]#

Return slices for indexing a rect in a numpy array.

Parameters:
posPoint

Position of rect.

sizeSize

Size of rect.

Returns:
tuple[slice, slice]

Slices that index a rect in a numpy array.

batgrl.geometry.basic.round_down(n: float) int#

Like the built-in round, but always rounds down.

Used instead of round for smoother geometry.