def apply_affine_transformation(self, vector: Vector) -> Vector: """ Apply the full affine transformation (linear + translation) to a vector. Generally used to transform points. Eg the center of an ellipse. """ vector_4d = Matrix([[vector.x], [vector.y], [1], [1]]) vector_4d = self.translation_matrix * vector_4d return Vector(vector_4d.matrix_list[0][0], vector_4d.matrix_list[1][0])
def add_scale(self, factor: float, factor_y=None): factor_x = factor factor_y = factor if factor_y is None else factor_y self.transformation_record.append(("scale", [factor_x, factor_y])) scale_matrix = Matrix([[factor_x, 0, 0, 0], [0, factor_y, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) self.translation_matrix *= scale_matrix
def add_rotation(self, angle: float): self.transformation_record.append(("rotate", [angle])) angle = math.radians(angle) rotation_matrix = Matrix([[math.cos(angle), -math.sin(angle), 0, 0], [math.sin(angle), math.cos(angle), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) self.translation_matrix *= rotation_matrix
def apply_linear_transformation(self, vector: Vector) -> Vector: """ Apply the linear component of the affine transformation (no translation) to a vector. Generally used to transform vector properties. Eg the radii of an ellipse. """ a = self.translation_matrix[0][0] b = self.translation_matrix[1][0] c = self.translation_matrix[0][1] d = self.translation_matrix[1][1] linear_transformation = Matrix([[a, c], [b, d]]) return linear_transformation * vector
def add_translation(self, x: float, y=0.0): self.transformation_record.append(("translate", [x, y])) translation_matrix = Matrix([[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, 0], [0, 0, 0, 1]]) self.translation_matrix *= translation_matrix
def add_matrix(self, a, b, c, d, e, f): self.transformation_record.append(("matrix", [a, b, c, d, e, f])) matrix = Matrix([[a, c, 0, e], [b, d, 0, f], [0, 0, 1, 0], [0, 0, 0, 1]]) self.translation_matrix *= matrix
def apply_transformation(self, point: Vector) -> Vector: point_4d = Matrix([[point.x], [point.y], [1], [1]]) point_4d = self.translation_matrix * point_4d return Vector(point_4d.matrix_list[0][0], point_4d.matrix_list[1][0])