"""
Point class

Medical Images 2022-2023 MUEI/MNUR ETSEIB
"""


class Point:
    """
    Represents a 2D point
    """

    EPS = 1e-5

    @classmethod
    def from_dict(cls, dic):
        """
        Creates a point from a dictionary with keys: x and y
        """
        return cls(dic["x"], dic["y"])

    def __init__(self, x, y):
        """
        Creates a point given its 2D coordinates
        """
        self.x = x
        self.y = y

    def __str__(self):
        """
        Returns a str indicating the coordinates of the point
        """
        return "Point ({:}, {:})".format(self.x, self.y)

    def __eq__(self, other):
        """
        Returns True if self has the same coordinates as other
        """
        return abs(self.x - other.x) < self.EPS and (self.y - other.y) < self.EPS

    def to_dict(self):
        """
        Returns a dictionary with the coordinates of self
        """
        return {"x": self.x, "y": self.y}

    def rasterization(self, matx):
        """
        rasterizes the point given the transformation matrix
        """
        return Point(
            int(self.x * matx[0][0] + self.y * matx[0][1] + 1 * matx[0][2]),
            int(self.x * matx[1][0] + self.y * matx[1][1] + 1 * matx[1][2]),
        )
