Skip to content

coordinate_2d

Bases: coordinate

Source code in tinybig/koala/geometry/coordinate.py
class coordinate_2d(coordinate):

    def __init__(self, h: int, w: int, *args, **kwargs):
        super().__init__(coords=(h, w), *args, **kwargs)

    def __add__(self, other):
        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):
        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):
        return self.coords[0] if self.dimension() > 0 else None

    @h.setter
    def h(self, value):
        if self.dimension() > 0:
            self.coords = (value,) + self.coords[1:]

    @property
    def w(self):
        return self.coords[1] if self.dimension() > 1 else None

    @w.setter
    def w(self, value):
        if self.dimension() > 1:
            self.coords = self.coords[0:1] + (value,) + self.coords[2:]

    @property
    def x(self):
        return self.h

    @x.setter
    def x(self, value):
        self.h = value

    @property
    def y(self):
        return self.w

    @y.setter
    def y(self, value):
        self.w = value