def test_arithmetic(): shape = Shape.circle([0, 0], 1, color=(0, 0, 0)) assert shape + [1, 10] == [1, 10] + shape == Shape.circle([1, 10], 1, color=(0, 0, 0)) assert shape - [1, 10] == Shape.circle([-1, -10], 1, color=(0, 0, 0)) assert 10 * shape == shape * 10 == Shape.circle([0, 0], 10, color=(0, 0, 0)) stretched = shape * np.array([2, 4]) assert [2, 4] * shape == stretched for point in stretched.vertices: assert np.isclose(np.linalg.norm(point / [2, 4]), 1)
def test_arithmetic(): shape = Shape.circle([0, 0], 1, color=(0, 0, 0)) assert shape + [1, 10] == [1, 10] + shape == Shape.circle( [1, 10], 1, color=(0, 0, 0)) assert shape - [1, 10] == Shape.circle([-1, -10], 1, color=(0, 0, 0)) assert 10 * shape == shape * 10 == Shape.circle( [0, 0], 10, color=(0, 0, 0)) stretched = shape * np.array([2, 4]) assert [2, 4] * shape == stretched for point in stretched.vertices: assert np.isclose(np.linalg.norm(point / [2, 4]), 1)
def test_scale(): shape = Shape.circle([0, 0], 1) shape.scale(10) assert shape == Shape.circle([0, 0], 10) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.scale([2, 5]) assert shape == Shape.rectangle([[-2, -5], [2, 5]]) shape = Shape.rectangle([[-1, -1], [1, 1]]) shape.scale(2, center=[1, 1]) assert shape == Shape.rectangle([[-3, -3], [1, 1]])
def main(screen=None): if screen: screens = pyglet.canvas.get_display().get_screens() window = pyglet.window.Window(screen=screens[screen], fullscreen=True) else: window = pyglet.window.Window(fullscreen=True) radius = np.diff(RADIUS_RANGE) * np.random.sample() + RADIUS_RANGE[0] shape_kwargs = { 'velocity': np.random.sample(2) * np.diff(VELOCITY_RANGE) + VELOCITY_RANGE[0] } center = [window.width / 2, window.height / 2] shapes = [Shape.circle(center, radius, **shape_kwargs)] shapes.extend( Shape.regular_polygon(center, radius, verts, start_angle=np.random.random() * 2 * np.pi / verts, **shape_kwargs) for verts in range(3, MAX_POLYGON_VERTICES + 1)) change_angular_velocity(shapes) for shape in shapes[1:]: shape.enable(False) window.set_handlers(on_draw=partial(draw, window, shapes), on_key_press=partial(on_key_press, shapes)) pyglet.clock.schedule(partial(update, window, shapes)) pyglet.app.run()
def test_repr(): shape = Shape.circle([0, 0], 1, velocity=[1, 1], color=(1, 2, 3), colors={'a': (20, 6, 169)}) assert eval(repr(shape)) == shape
def test_update(): shape = Shape.circle([0, 0], 1, velocity=[1, 1]) shape.update(1) assert np.all(np.isclose(shape.center, [1, 1])) shape = Shape.regular_polygon([0, 0], 1, 6, angular_velocity=1, velocity=[-2, 2]) shape.update(0.5) assert shape == Shape.regular_polygon([-1, 1], 1, 6, angular_velocity=1, velocity=[-2, 2], start_angle=0.5)
def test_colors(): colors = { 'primary': (0, 0, 0), 'secondary': (100, 100, 100), 'flashing': (20, 10, 89), } shape = Shape.circle([0, 0], 1, colors=colors) assert shape.color == 'primary' shape.color = 'secondary' assert shape.color == 'secondary' shape.color = (72, 71, 8) assert shape.color == 'secondary' shape.color = 'flashing' assert shape.colors['secondary'] == (72, 71, 8)
def on_draw(self): self.clear() self.beatradius = int(self.width * 0.045 * 0.5) self.beaty = int(self.height - (self.beatradius * 2.5)) self.barheight = int(self.height - (self.beatradius * 4)) self.barwidth = int(self.width * 0.06) self.begin = int(self.barwidth * 0.5) shapes = [] x = self.begin width = self.barwidth for bin in "bass", "mid", "tre": d = self.data["bins"][bin] level = int(d["level"] * self.barheight) flux = int(d["flux"] * self.barheight) trans = int(d["transient"] * 255) if not self.data["silence"]: shapes.append(Shape.circle([x + self.beatradius, self.beaty], self.beatradius, color=(0, trans, 0))) shapes.append( Shape.rectangle([(x, 0), (x+width, level)], color=(255, 0, 0))) x += width shapes.append( Shape.rectangle([(x, 0), (x+width, flux)], color=(255, 128, 0))) x += int(width * 1.5) x += self.barwidth width = int((self.width - x) / (len(self.data["spectrum"]) + 1)) g = 0 for level in self.data["spectrum"]: level = int(level * self.barheight) shapes.append( Shape.rectangle([(x, 0), (x+width, level)], color=(0, g, 255))) g = min(g + 20, 255) x += width for shape in shapes: shape.draw()
def main(screen=None): if screen: screens = pyglet.canvas.get_display().get_screens() window = pyglet.window.Window(screen=screens[screen], fullscreen=True) else: window = pyglet.window.Window(fullscreen=True) radius = np.diff(RADIUS_RANGE) * np.random.sample() + RADIUS_RANGE[0] shape_kwargs = {'velocity': np.random.sample(2) * np.diff(VELOCITY_RANGE) + VELOCITY_RANGE[0]} center = [window.width/2, window.height/2] shapes = [Shape.circle(center, radius, **shape_kwargs)] shapes.extend(Shape.regular_polygon(center, radius, verts, start_angle=np.random.random() * 2 * np.pi / verts, **shape_kwargs) for verts in range(3, MAX_POLYGON_VERTICES + 1)) change_angular_velocity(shapes) for shape in shapes[1:]: shape.enable(False) window.set_handlers(on_draw=partial(draw, window, shapes), on_key_press=partial(on_key_press, shapes)) pyglet.clock.schedule(partial(update, window, shapes)) pyglet.app.run()
def test_overlaps(): a = Shape.circle([-1, 0], 1) b = a + [2, 0] assert not a.overlaps(b) a += [1.01, 0] assert a.overlaps(b)
def test_translate(): shape = Shape.circle([0, 0], 1) shape.translate([10, 10]) assert shape == Shape.circle([10, 10], 1)
def test_from_polygon(): shape = Shape.circle([0, 0], 1) assert shape == Shape(shape.poly)
def test_bool(): assert Shape.circle([0, 0], 1)
def test_circle_from_dict(): spec = { 'center': [0, 0], 'radius': 1, } assert Shape.from_dict(spec) == Shape.circle([0, 0], 1)
def test_distance_to(): shape = Shape.circle([0, 0], 1) assert np.isclose(shape.distance_to([1, 1]), np.sqrt(2))
def test_color(): shape = Shape.circle([0, 0], 1, color=(1, 2, 3)) assert shape.color == (1, 2, 3) shape.color = (10, 20, 30) assert shape.color == (10, 20, 30)
def test_covers(): a = Shape.circle([-1, 0], 1) b = a / 2 + [0.5, 0.5] assert not a.covers(b) a *= 2 assert a.covers(b)
def test_radius(): shape = Shape.circle([0, 0], 1) assert shape.radius == 1 shape.radius = 0.1 assert shape == Shape.circle([0, 0], 0.1)
def test_circle(): assert Shape.circle([0, 0], 1, n_vertices=50) == Shape.regular_polygon([0, 0], 1, 50)
def test_neq(): assert Shape.circle([0, 0], 1) != 0