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:
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:
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: T, min: T | None, max: T | None) T#

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:
valueT

Value to clamp.

minT | None

Minimum of clamped value.

maxT | None

Maximum of clamped value.

Returns:
T

A value between min and max, inclusive.

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

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[tuple[int, Literal[2]], dtype[float64]]#

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:
Coords

An (n, 2)-shaped array of evenly-spaced points on a circle.

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

Return slices for indexing a rect in a numpy array.

Parameters:
posPointlike

Position of rect.

sizeSizelike

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.