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
Coord2finstance, in which case a newCoord2fis returned with the same coordinate values.y – Y coordinate if
x_or_xyis not a tuple. ATypeErroris raised ifyis supplied a not-Nonevalue whenx_or_xyis a tuple.
- as_int(round_fn: Callable[[float], int] | None = None) Coord2i¶
Returns this
Coord2fas a :py:class:~squaremap_combine.geo.Coord2i, with both coordinates rounded withround_fn. By default, int() is called on both values.
- as_tuple() tuple[float, float]¶
Returns the coordinate as a tuple.
- 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
xandyare typed as onlyint, and explicit conversion should be used, afloatis still technically accepted (and converted toint) if it is a whole number, i.e. if ends with.0. Otherwise, aValueErroris 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
Coord2iwith 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
Coord2iinstance, in which case a newCoord2iis returned with the same coordinate values.y – Y coordinate if
x_or_xyis not a tuple. ATypeErroris raised ifyis supplied a not-Nonevalue whenx_or_xyis a tuple.
- as_tuple() tuple[int, int]¶
Returns the coordinate as a tuple.
- 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
Gridwith 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
Gridwith bounds defined by a given set of X,Y coordinate steps.
- iter_steps() Generator[Coord2i]¶
Yields
Coord2iobjects from a :py:class:~itertools.productiterator 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
Gridwithfnapplied to all coordinate values of itsrect.- 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 theGrid’s origin without any modification.
- project(coord: Coord2i | tuple[int, int], other_grid: Grid) Coord2i¶
Returns a
Coord2ifrom thisGridas if it were at the same relative position onother_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
Gridresized by xy, based off of this instance’srectsize. Refer toresize().
- 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
coordis(4, 7), and thisGrid’sstepis 10,.snap_coord(coord)would returnCoord2i(0, 10).- Parameters:
round_fn – A function of
(int) -> intto use for rounding the divided value in the formula. By default the built-inroundis used, butmath.floorormath.ceilfor 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
Gridwith this instance’srectcoordinates shifted byxy. Refer totranslate_by().
- translate_to(xy: Coord2i | tuple[int, int]) Grid¶
Returns a new
Gridwith this instance’srectcoordinates shifted such that its top left coordinate equalsxy. Refer totranslate_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
Rectobject.
- as_tuple() tuple[int, int, int, int]¶
Returns the X1, Y1, X2, and Y2 values as a
tuple.
- property corners: tuple[Coord2i, Coord2i, Coord2i, Coord2i]¶
Returns the four corner coordinates as
Coord2iobjects.- 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
Rectbased on a given radius and origin coordinate.
- classmethod from_size(size: tuple[int, int], center: Coord2i | tuple[int, int] | None) Self¶
Returns a new
Rectof the given size. Will be created with its top left coordinate at0, 0by default unlesscenteris specified.
- in_bounds(coord: Coord2i | Coord2f | tuple[int | float, int | float]) bool¶
Returns whether
coordis within thisRectobject’s bounds.
- resize(xy: Coord2i | tuple[int, int] | int = 0, *, from_center: bool = False) Rect¶
Returns a new
Rectresized by xy, based off of this instance’s size. By default, theRectis resized from the top-left corner, keeping its coordinate intact and only adding to the bottom-right coordinate. Iffrom_centerisTrue, it will be resized outward in all directions from the center of theRect.
- translate_by(xy: Coord2i | tuple[int, int] | int = 0) Rect¶
Returns a new
Rectwith this instance’s coordinates shifted byxy.
- translate_to(xy: Coord2i | tuple[int, int]) Rect¶
Returns a new
Rectwith this instance’s coordinates shifted such that its top left coordinate equalsxy.
- x1: int¶
Top-left X coordinate.
- x2: int¶
Bottom-right X coordinate.
- y1: int¶
Top-left Y coordinate.
- y2: int¶
Bottom-right Y coordinate.