Skip to content

coordinate_2d

Bases: coordinate

Represents a coordinate in a 2D space, extending the general coordinate class.

Attributes:

Name Type Description
coords tuple[int, int]

The tuple representing the 2D coordinate.

h int

The height coordinate.

w int

The width coordinate.

x, y int

Aliases for h and w respectively.

Methods:

Name Description
__add__

Adds another 2D coordinate to this one.

__sub__

Subtracts another 2D coordinate from this one.

Source code in tinybig/koala/geometry/coordinate.py
class coordinate_2d(coordinate):
    """
        Represents a coordinate in a 2D space, extending the general coordinate class.

        Attributes
        ----------
        coords : tuple[int, int]
            The tuple representing the 2D coordinate.
        h : int
            The height coordinate.
        w : int
            The width coordinate.
        x, y : int
            Aliases for h and w respectively.

        Methods
        -------
        __add__(other)
            Adds another 2D coordinate to this one.
        __sub__(other)
            Subtracts another 2D coordinate from this one.
    """
    def __init__(self, h: int, w: int, *args, **kwargs):
        """
            Initializes a 2D coordinate object.

            Parameters
            ----------
            h : int
                The height coordinate.
            w : int
                The width coordinate.
            *args, **kwargs
                Additional arguments for customization.

            Attributes
            ----------
            coords : tuple[int, int]
                Stores the 2D coordinate as a tuple.
        """
        super().__init__(coords=(h, w), *args, **kwargs)

    def __add__(self, other):
        """
            Adds another 2D coordinate to this one.

            Parameters
            ----------
            other : coordinate_2d
                The 2D coordinate to add.

            Returns
            -------
            coordinate_2d
                The resulting coordinate after addition.

            Raises
            ------
            TypeError
                If the operands are not of the same type or dimensions.
        """
        if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
            return self.__class__(*tuple(a + b for a, b in zip(self.coords, other.coords)))
        raise TypeError("Operands must be of type Coordinate and have the same dimensions")

    # Redefine the - operator
    def __sub__(self, other):
        """
            Subtracts another 2D coordinate from this one.

            Parameters
            ----------
            other : coordinate_2d
                The 2D coordinate to subtract.

            Returns
            -------
            coordinate_2d
                The resulting coordinate after subtraction.

            Raises
            ------
            TypeError
                If the operands are not of the same type or dimensions.
        """
        if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
            return self.__class__(*tuple(a - b for a, b in zip(self.coords, other.coords)))
        raise TypeError("Operands must be of type Coordinate and have the same dimensions")

    @property
    def h(self):
        """
            The height coordinate.

            Returns
            -------
            int
                The height value.
        """
        return self.coords[0] if self.dimension() > 0 else None

    @h.setter
    def h(self, value):
        """
            Sets the height coordinate.

            Parameters
            ----------
            value : int
                The new value for the height coordinate.
        """
        if self.dimension() > 0:
            self.coords = (value,) + self.coords[1:]

    @property
    def w(self):
        """
            The width coordinate.

            Returns
            -------
            int
                The width value.
        """
        return self.coords[1] if self.dimension() > 1 else None

    @w.setter
    def w(self, value):
        """
            Sets the width coordinate.

            Parameters
            ----------
            value : int
                The new value for the width coordinate.
        """
        if self.dimension() > 1:
            self.coords = self.coords[0:1] + (value,) + self.coords[2:]

    @property
    def x(self):
        """
            Alias for h (height).
        """
        return self.h

    @x.setter
    def x(self, value):
        """
            Alias for setting the height coordinate.

            Parameters
            ----------
            value : int
                The new value for the height coordinate.
        """
        self.h = value

    @property
    def y(self):
        """
            Alias for w (width).
        """
        return self.w

    @y.setter
    def y(self, value):
        """
            Alias for setting the width coordinate.

            Parameters
            ----------
            value : int
                The new value for the width coordinate.
        """
        self.w = value

h property writable

The height coordinate.

Returns:

Type Description
int

The height value.

w property writable

The width coordinate.

Returns:

Type Description
int

The width value.

x property writable

Alias for h (height).

y property writable

Alias for w (width).

__add__(other)

Adds another 2D coordinate to this one.

Parameters:

Name Type Description Default
other coordinate_2d

The 2D coordinate to add.

required

Returns:

Type Description
coordinate_2d

The resulting coordinate after addition.

Raises:

Type Description
TypeError

If the operands are not of the same type or dimensions.

Source code in tinybig/koala/geometry/coordinate.py
def __add__(self, other):
    """
        Adds another 2D coordinate to this one.

        Parameters
        ----------
        other : coordinate_2d
            The 2D coordinate to add.

        Returns
        -------
        coordinate_2d
            The resulting coordinate after addition.

        Raises
        ------
        TypeError
            If the operands are not of the same type or dimensions.
    """
    if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
        return self.__class__(*tuple(a + b for a, b in zip(self.coords, other.coords)))
    raise TypeError("Operands must be of type Coordinate and have the same dimensions")

__init__(h, w, *args, **kwargs)

Initializes a 2D coordinate object.

Parameters:

Name Type Description Default
h int

The height coordinate.

required
w int

The width coordinate.

required
*args

Additional arguments for customization.

()
**kwargs

Additional arguments for customization.

()

Attributes:

Name Type Description
coords tuple[int, int]

Stores the 2D coordinate as a tuple.

Source code in tinybig/koala/geometry/coordinate.py
def __init__(self, h: int, w: int, *args, **kwargs):
    """
        Initializes a 2D coordinate object.

        Parameters
        ----------
        h : int
            The height coordinate.
        w : int
            The width coordinate.
        *args, **kwargs
            Additional arguments for customization.

        Attributes
        ----------
        coords : tuple[int, int]
            Stores the 2D coordinate as a tuple.
    """
    super().__init__(coords=(h, w), *args, **kwargs)

__sub__(other)

Subtracts another 2D coordinate from this one.

Parameters:

Name Type Description Default
other coordinate_2d

The 2D coordinate to subtract.

required

Returns:

Type Description
coordinate_2d

The resulting coordinate after subtraction.

Raises:

Type Description
TypeError

If the operands are not of the same type or dimensions.

Source code in tinybig/koala/geometry/coordinate.py
def __sub__(self, other):
    """
        Subtracts another 2D coordinate from this one.

        Parameters
        ----------
        other : coordinate_2d
            The 2D coordinate to subtract.

        Returns
        -------
        coordinate_2d
            The resulting coordinate after subtraction.

        Raises
        ------
        TypeError
            If the operands are not of the same type or dimensions.
    """
    if isinstance(other, self.__class__) and len(self.coords) == len(other.coords):
        return self.__class__(*tuple(a - b for a, b in zip(self.coords, other.coords)))
    raise TypeError("Operands must be of type Coordinate and have the same dimensions")