geo module

Geometry classes and functions.

class squaremap_combine.geo.Coord2f(x_or_xy: int | float, y: int | float)
class squaremap_combine.geo.Coord2f(x_or_xy: tuple[int | float, int | float] | Coord2i | Coord2f, y: None = None)

Represents a 2D float coordinate pair.

__init__(x_or_xy: int | float, y: int | float) None
__init__(x_or_xy: tuple[int | float, int | float] | Coord2i | Coord2f, y: None = None) None
Parameters:
  • x_or_xy – Either a tuple of two floats, or an X coordinate. Can also be another Coord2f instance, in which case a new Coord2f is returned with the same coordinate values.

  • y – Y coordinate if x_or_xy is not a tuple. A TypeError is raised if y is supplied a not-None value when x_or_xy is a tuple.

as_int(round_fn: Callable[[float], int] | None = None) Coord2i

Returns this Coord2f as a :py:class:~squaremap_combine.geo.Coord2i, with both coordinates rounded with round_fn. By default, int() is called on both values.

as_tuple() tuple[float, float]

Returns the coordinate as a tuple.

in_bounds(rect: Rect | tuple[int, int, int, int]) bool

Returns whether this coordinate is within the given rect’s bounds.

map(fn: Callable[[float], float]) Coord2f

Returns a new Coord2f instance with fn applied to its x and y attributes.

class squaremap_combine.geo.Coord2i(x_or_xy: int, y: int)
class squaremap_combine.geo.Coord2i(x_or_xy: tuple[int, int] | Coord2i, y: None = None)

Represents a 2D integer coordinate pair. While x and y are typed as only int, and explicit conversion should be used, a float is still technically accepted (and converted to int) if it is a whole number, i.e. if ends with .0. Otherwise, a ValueError is raised on initialization.

__init__(x_or_xy: int, y: int) None
__init__(x_or_xy: tuple[int, int] | Coord2i, y: None = None) None

Initializes a Coord2i with either a single tuple argument, or two coordinate arguments.

Parameters:
  • x_or_xy – Either a tuple of two integers, or an X coordinate. Can also be another Coord2i instance, in which case a new Coord2i is returned with the same coordinate values.

  • y – Y coordinate if x_or_xy is not a tuple. A TypeError is raised if y is supplied a not-None value when x_or_xy is a tuple.

as_tuple() tuple[int, int]

Returns the coordinate as a tuple.

in_bounds(rect: Rect | tuple[int, int, int, int]) bool

Returns whether this coordinate is within the given rect’s bounds.

map(fn: Callable[[int], int]) Coord2i

Returns a new Coord2i instance with fn applied to its x and y attributes.

class squaremap_combine.geo.Grid(rect: Rect | tuple[int, int, int, int], *, step: int = 0, origin: Coord2i | tuple[int, int] = (0, 0))

Represents a 2D grid with defined corners and a step value.

__init__(rect: Rect | tuple[int, int, int, int], *, step: int = 0, origin: Coord2i | tuple[int, int] = (0, 0)) None
copy(*, step: int | None = None) Grid

Returns a new Grid with the same coordinates and step value (unless specified otehrwise) as this instance.

classmethod from_steps(source_steps: Iterable[Coord2i | tuple[int, int]], step: int = 0) Self

Returns a Grid with bounds defined by a given set of X,Y coordinate steps.

iter_steps() Generator[Coord2i]

Yields Coord2i objects from a :py:class:~itertools.product iterator of the x- and y-axis steps.

map(fn: Callable[[int], int], *, origin: Coord2i | tuple[int, int] | Literal['keep'] | None = None) Grid

Returns a new Grid with fn applied to all coordinate values of its rect.

Parameters:

origin – A new origin to give to the resulting grid. If None (default), the origin is automatically re-calculated based off of its relative distance from the top left corner. Alternatively, 'keep' can be given to preserve the Grid’s origin without any modification.

origin: Coord2i

The origin point from which grid steps should be counted outward from.

project(coord: Coord2i | tuple[int, int], other_grid: Grid) Coord2i

Returns a Coord2i from this Grid as if it were at the same relative position on other_grid.

>>> g1 = Grid((-100, -100, 100, 100))
>>> g2 = Grid((0, 0, 100, 100))
>>> assert g1.transpose_coord(Coord2i(0, 0), g2) == Coord2i(50, 50)
resize(xy: Coord2i | tuple[int, int] | int = 0, *, from_center: bool = False) Grid

Returns a new Grid resized by xy, based off of this instance’s rect size. Refer to resize().

snap_coord(coord: Coord2i | tuple[int, int], round_fn: Callable[[int | float], int] | None = None) Coord2i

Returns the nearest grid interval coordinate for a given coordinate. For example; where coord is (4, 7), and this Grid’s step is 10, .snap_coord(coord) would return Coord2i(0, 10).

Parameters:

round_fn – A function of (int) -> int to use for rounding the divided value in the formula. By default the built-in round is used, but math.floor or math.ceil for example could be used to snap to the lower or higher coordinate point respectively.

step: int

An interval to count steps or divisions in the grid by.

property steps_count: int

The total number of X,Y coordinates that exist on this grid by step.

translate_by(xy: Coord2i | tuple[int, int] | int = 0) Grid

Returns a new Grid with this instance’s rect coordinates shifted by xy. Refer to translate_by().

translate_to(xy: Coord2i | tuple[int, int]) Grid

Returns a new Grid with this instance’s rect coordinates shifted such that its top left coordinate equals xy. Refer to translate_to().

class squaremap_combine.geo.Rect(coords_or_rect: Rect | tuple[int, int, int, int])

Represents a simple 2D rectangle.

__init__(coords_or_rect: Rect | tuple[int, int, int, int]) None
Parameters:

coords_or_rect – A tuple of rectangle coordinates (top-left to bottom-right, i.e. X1, Y1, X2, Y2), or another Rect object.

as_tuple() tuple[int, int, int, int]

Returns the X1, Y1, X2, and Y2 values as a tuple.

property center: Coord2i

The center coordinate.

copy() Rect

Returns a new Rect with the same coordinates as this instance.

property corners: tuple[Coord2i, Coord2i, Coord2i, Coord2i]

Returns the four corner coordinates as Coord2i objects.

Returns corners:

(top-left, top-right, bottom-left, bottom-right)

classmethod from_radius(radius: int | Coord2i | tuple[int, int], center: Coord2i | tuple[int, int] | None = (0, 0)) Self

Returns a new Rect based on a given radius and origin coordinate.

classmethod from_size(size: tuple[int, int], center: Coord2i | tuple[int, int] | None) Self

Returns a new Rect of the given size. Will be created with its top left coordinate at 0, 0 by default unless center is specified.

in_bounds(coord: Coord2i | Coord2f | tuple[int | float, int | float]) bool

Returns whether coord is within this Rect object’s bounds.

map(fn: Callable[[int], int]) Rect

Returns a new Rect with fn applied to all coordinate values.

resize(xy: Coord2i | tuple[int, int] | int = 0, *, from_center: bool = False) Rect

Returns a new Rect resized by xy, based off of this instance’s size. By default, the Rect is resized from the top-left corner, keeping its coordinate intact and only adding to the bottom-right coordinate. If from_center is True, it will be resized outward in all directions from the center of the Rect.

translate_by(xy: Coord2i | tuple[int, int] | int = 0) Rect

Returns a new Rect with this instance’s coordinates shifted by xy.

translate_to(xy: Coord2i | tuple[int, int]) Rect

Returns a new Rect with this instance’s coordinates shifted such that its top left coordinate equals xy.

x1: int

Top-left X coordinate.

x2: int

Bottom-right X coordinate.

y1: int

Top-left Y coordinate.

y2: int

Bottom-right Y coordinate.