def test_inequality(self): a = Coordinate2(1, 2) b = Coordinate2(3, 2) c = Coordinate2(1, 4) self.assertTrue(a != b) self.assertTrue(a != c)
def neighbours(self, pos): """Returns an iterator over all coordinates adjacent to a point. These do not have to be linked. """ x, y = pos return { Coordinate2(x - 1, y), Coordinate2(x, y - 1), Coordinate2(x + 1, y), Coordinate2(x, y + 1), }
def add_link(self, a: Coordinate2, b: Coordinate2): """Adds a single step, horizontal or vertical link between two, drilled holes. """ # Vertical link if a.x == b.x: if abs(b.y - a.y) != 1: raise ValueError("Can only link adjacent nodes") self.__y_links.add(Coordinate2(a.x, min(a.y, b.y))) # Horizontal link elif a.y == b.y: if abs(b.x - a.x) != 1: raise ValueError("Can only link adjacent nodes") self.__x_links.add(Coordinate2(min(a.x, b.x), a.y)) else: raise ValueError("Links must be either horizontal or vertical")
def connected(self, pos): """Returns an iterator over all of the points that are connected to the origin by a link. """ connected = set() x, y = pos if Coordinate2(x - 1, y) in self.__x_links: connected.add(Coordinate2(x - 1, y)) if Coordinate2(x, y - 1) in self.__y_links: connected.add(Coordinate2(x, y - 1)) if Coordinate2(x, y) in self.__x_links: connected.add(Coordinate2(x + 1, y)) if Coordinate2(x, y) in self.__y_links: connected.add(Coordinate2(x, y + 1)) return connected
def test_equality(self): a = Coordinate2(1, 2) b = Coordinate2(1, 2) self.assertTrue(a == b)
def test_subtract_from_vector(self): c = Coordinate2(1, 2) v = Vector2(3, 5) with self.assertRaises(TypeError): v - c
def test_subtract_vector(self): c = Coordinate2(1, 2) v = Vector2(3, 5) self.assertEqual(c - v, Coordinate2(-2, -3))
def test_subtract_coordinate(self): a = Coordinate2(1, 2) b = Coordinate2(3, 5) self.assertEqual(b - a, Vector2(2, 3))
def test_add_vector(self): a = Coordinate2(100, 50) b = Vector2(1, 1) self.assertEqual(a + b, Coordinate2(101, 51)) self.assertEqual(b + a, Coordinate2(101, 51))
def test_add_coordinate(self): a = Coordinate2(1, 2) b = Coordinate2(3, 5) with self.assertRaises(TypeError): a + b
def test_equality(self): a = _HalfEdge(Coordinate2(1, 2), Coordinate2(1, 3)) b = _HalfEdge(Coordinate2(1, 2), Coordinate2(1, 3)) self.assertTrue(a == b)
def test_construct_from_kwargs(self): c = Coordinate2(x=1, y=2) self.assertEqual(c.x, 1) self.assertEqual(c.y, 2)
def links(self): for origin in self.__x_links: yield _Link(self, origin, Coordinate2(origin.x + 1, origin.y)) for origin in self.__y_links: yield _Link(self, origin, Coordinate2(origin.x, origin.y + 1))
def load_gif(filename, *, config): gif = PIL.Image.open(filename) grid = config.get('grid', 2.0) layers = [] for image, layer_config in zip(PIL.ImageSequence.Iterator(gif), config['layers']): name: Optional[str] = layer_config.get('name') material: str = layer_config.get('material', 'acrylic') thickness: float = layer_config.get('thickness', 2.0) width, height = image.size transparency = image.info['transparency'] radius_lookup = {} for y, channel in enumerate(config['channels']): colour = image.getpixel((0, y)) if colour == transparency: continue radius_lookup[colour] = channel['radius'] layer = Layer( name=name, material=material, thickness=thickness, grid=grid, width=width, height=height, ) for x in range(1, width): for y in range(height): if image.getpixel((x, y)) == transparency: continue if x < 2 or x > width - 2 or y < 2 or x > height - 2: raise Exception("out of bounds") radius = radius_lookup[image.getpixel((x, y))] above = image.getpixel((x, y - 1)) != transparency below = image.getpixel((x, y + 1)) != transparency left = image.getpixel((x - 1, y)) != transparency right = image.getpixel((x + 1, y)) != transparency # Pixel has no neighbours. if not any([above, below, left, right]): layer.add_hole(Coordinate2(x, y), radius=radius) if right: layer.add_link( Coordinate2(x, y), Coordinate2(x + 1, y), ) if below: layer.add_link( Coordinate2(x, y), Coordinate2(x, y + 1), ) layers.append(layer) return layers
def test_negate(self): c = Coordinate2(12, 12) with self.assertRaises(TypeError): -c # pylint: disable=invalid-unary-operand-type
def test_hash(self): a = Coordinate2(1, 2) b = Coordinate2(1, 2) self.assertTrue(hash(a) == hash(b))
def test_lookup(self): s = {_HalfEdge(Coordinate2(1, 2), Coordinate2(1, 3))} self.assertIn(_HalfEdge(Coordinate2(1, 2), Coordinate2(1, 3)), s)