def test_clamp_point_moves_a_point_outside_the_rectangle_to_the_nearest_border(self): # noqa r = Rectangle(Point(-2, -3), Size(5, 7)) self.assertEqual(r.clamp_point(Point(-5, 0)), Point(-2, 0)) self.assertEqual(r.clamp_point(Point(0, -6)), Point(0, -3)) self.assertEqual(r.clamp_point(Point(6, 0)), Point(3, 0)) self.assertEqual(r.clamp_point(Point(0, 8)), Point(0, 4)) self.assertEqual(r.clamp_point(Point(100, 100)), Point(3, 4))
def pan(self, v): scaled_v = self.convert_world_vector_to_view_vector(v) center = self.view_rect.clamp_point(self.view_rect.center + scaled_v) clamped_v = center - self.view_rect.center self.view_rect = Rectangle( self.view_rect.p + clamped_v, self.view_rect.sz, )
class Camera: def __init__(self, tile_map): self.tile_map = tile_map self.view_rect = Rectangle(Point(0, 0), tile_map.sz) self.minimum_tile_line_in_view = 10 def convert_world_vector_to_view_vector(self, v): return (v / self.tile_map.sz) * self.view_rect.sz def convert_world_point_to_view_point(self, p): return (p / self.tile_map.sz) * self.view_rect.sz + self.view_rect.p def pan(self, v): scaled_v = self.convert_world_vector_to_view_vector(v) center = self.view_rect.clamp_point(self.view_rect.center + scaled_v) clamped_v = center - self.view_rect.center self.view_rect = Rectangle( self.view_rect.p + clamped_v, self.view_rect.sz, ) def zoom(self, z): min_sz = self.tile_map.tile_sz * self.minimum_tile_line_in_view min_z = max(*(min_sz / self.view_rect.sz)) max_sz = self.tile_map.sz max_z = min(*(max_sz / self.view_rect.sz)) clamped_z = max(min(z, max_z), min_z) self.view_rect = self.view_rect * clamped_z @property def ortho_matrix(self): return ( self.view_rect.left, self.view_rect.right, self.view_rect.bottom, self.view_rect.top, )
def __init__(self, tile_map): self.tile_map = tile_map self.view_rect = Rectangle(Point(0, 0), tile_map.sz) self.minimum_tile_line_in_view = 10
def test_translate_returns_a_rectangle_that_has_been_translated_over_a_vector(self): # noqa r = Rectangle(Point(2, 3), Size(3, 4)) self.assertEqual( r.translate(Vector(2, 3)), Rectangle(Point(4, 6), Size(3, 4)), )
def test_translate_y_returns_a_rectangle_that_has_been_translated_by_distance_in_y(self): # noqa r = Rectangle(Point(2, 3), Size(3, 4)) self.assertEqual(r.translate_y(3), Rectangle(Point(2, 6), Size(3, 4)))
def test_scale_with_center_set_to_false_returns_a_rectangle_with_its_width_and_height_multiplied_by_a_scale_factor_anchored_to_the_same_point_as_the_source_rectangle(self): # noqa r = Rectangle(Point(2, 3), Size(3, 4)) self.assertEqual( r.scale(3, center=False), Rectangle(Point(2, 3), Size(9, 12)), )
def test_scale_returns_a_rectangle_with_its_width_and_height_multiplied_by_a_scale_factor_centered_on_the_same_center_as_the_source_rectangle(self): # noqa r = Rectangle(Point(2, 3), Size(3, 4)) self.assertEqual( r.scale(3), Rectangle(Point(2-(9-3)/2, 3-(12-4)/2), Size(9, 12)), )