def from_image_path(cls, image_path, center_offset, size): """Creates ImageGraphic from an image file. Scales image to match the `size` aspect ratio. Arguments: `image_path`: pathlib.Path object Denotes the path to the image. `center_offset`: Vector2 The position of the center of the image relative to the `location` of the ImageGraphic `size`: Vector2 The dimensions of the image Returns: An ImageGraphic object """ helper_rect = FloatRect(0, 0, size[0], size[1]) helper_rect.center = (math.floor(center_offset[0]), math.floor(center_offset[1])) rectangle = Rectangle.from_rect(helper_rect) try: image = Image(image_path) except Exception: logging.critical(f"Failed loading image from {image_path}.") logging.critical(f"Are the configuration files OK?") sys.exit() image.set_aspect_ratio(helper_rect.width, helper_rect.height) return ImageGraphic(rectangle, image)
def test_location_plus_rotation_change_endpoints_correctly(self, rect1): rect1.location = Vector2(1, 3) rect1.rotation = math.pi / 2 assert rect_eq( rect1, Rectangle(Vector2(1, 3), Vector2(0 + 1, -2 + 3), Vector2(1 + 1, 0 + 3)))
def image_graphic(self, image): rectangle = Rectangle(Vector2(0, 0), Vector2(3, 0), Vector2(0, 2)) return ImageGraphic(rectangle, image)
def test_polyline_rectangle_intersection_polyline_inside_rectangle( self, polyline1): rectangle = Rectangle(Vector2(0, 0), Vector2(10, 0), Vector2(0, 10)) assert polyline1.intersects(rectangle) assert rectangle.intersects(polyline1)
def test_polyline_rectangle_intersection(self, polyline1): rectangle = Rectangle(Vector2(2, 3), Vector2(2.2, 3), Vector2(2, 3.3)) assert polyline1.intersects(rectangle) assert rectangle.intersects(polyline1)
def test_polyline_rectangle_no_intersection(self, polyline1): rectangle = Rectangle(Vector2(0, 0), Vector2(1, 0), Vector2(0, 1)) assert not polyline1.intersects(rectangle) assert not rectangle.intersects(polyline1)
def test_rectangle_rectangle_intersection_fully_inside_other(self): rect1 = Rectangle(Vector2(1, 0), Vector2(2, 0), Vector2(1, 1)) rect2 = Rectangle(Vector2(1.1, 0.1), Vector2(1.2, 0.1), Vector2(1.1, 0.3)) assert rect1.intersects(rect2) assert rect2.intersects(rect1)
def test_rectangle_rectangle_intersection(self): rect1 = Rectangle(Vector2(1, 0), Vector2(2, 0), Vector2(1, 1)) rect2 = Rectangle(Vector2(0.5, 0), Vector2(3, 0), Vector2(0.5, 1)) assert rect1.intersects(rect2) assert rect2.intersects(rect1)
def test_rectangle_circle_intersection_center_not_inside_rect( self, unit_circle): rect = Rectangle(Vector2(0.8, 0), Vector2(2, 0), Vector2(0.8, 1)) assert unit_circle.intersects(rect) assert rect.intersects(unit_circle)
def test_rectangle_circle_intersection_center_inside_rect( self, unit_circle): rect = Rectangle(Vector2(-1, -1), Vector2(1, -1), Vector2(-1, 1)) assert unit_circle.intersects(rect) assert rect.intersects(unit_circle)
def test_rectangle_circle_no_intersection(self, unit_circle): rect = Rectangle(Vector2(1.1, 0), Vector2(2, 0), Vector2(1.1, 1)) assert not unit_circle.intersects(rect) assert not rect.intersects(unit_circle)
def test_setting_rotation_moves_sides_correctly(self, rect1): rect1.rotation = math.pi / 2 assert rect_eq(rect1, Rectangle(Vector2(0, 0), Vector2(0, -2), Vector2(1, 0)))
def test_setting_location_moves_sides_correctly(self, rect1): rect1.location = Vector2(1, 3) assert rect_eq(rect1, Rectangle(Vector2(1, 3), Vector2(3, 3), Vector2(1, 4)))
def test_constructor_fails_when_not_rectangular(self): with pytest.raises(ValueError) as e: Rectangle(Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)) assert "Arguments do not form a rectangle" == str(e.value)
def test_constructor_fails_when_degenerate(self): with pytest.raises(ValueError) as e: Rectangle(Vector2(0, 0), Vector2(0, 0), Vector2(1, 0)) assert "Degenerate Rectangles are not allowed" == str(e.value)
def rect1(self): return Rectangle(Vector2(0, 0), Vector2(2, 0), Vector2(0, 1))