Exemple #1
0
def test_star():
    surface = gz.Surface(200, 200, bg_color=(1, 0.9, 0.6))

    star1 = gz.star(radius=70, ratio=.4, fill=(1, 1, 1), angle=-np.pi / 2,
                    stroke_width=2, stroke=(1, 0, 0))
    star2 = gz.star(radius=55, ratio=.4, fill=(1, 0, 0), angle=-np.pi / 2)
    stars = gz.Group([star1, star2]).translate([100, 100])
    stars.draw(surface)

    assert is_like_sample(surface, 'star')
Exemple #2
0
def update_gizeh_image():
    star1 = gz.star(radius=70,
                    ratio=.4,
                    fill=(1, 1, 1),
                    angle=-np.pi / 2,
                    stroke_width=2,
                    stroke=(1, 0, 0))
    star2 = gz.star(radius=55, ratio=.4, fill=(1, 0, 0), angle=-np.pi / 2)
    # Gizeh coords are right-down.
    stars = gz.Group([star1, star2]).translate(
        [random.randint(100, 412),
         random.randint(100, 412)])
    stars.draw(surface)
Exemple #3
0
def test_star():
    surface = gz.Surface(200, 200, bg_color=(1, 0.9, 0.6))

    star1 = gz.star(radius=70,
                    ratio=.4,
                    fill=(1, 1, 1),
                    angle=-np.pi / 2,
                    stroke_width=2,
                    stroke=(1, 0, 0))
    star2 = gz.star(radius=55, ratio=.4, fill=(1, 0, 0), angle=-np.pi / 2)
    stars = gz.Group([star1, star2]).translate([100, 100])
    stars.draw(surface)

    assert is_like_sample(surface, 'star')
Exemple #4
0
def test_pdfsurface():
    """Test PDFSurface class."""
    # 800x800 point image
    im_size = 800

    # Create a simple star shape with a fill
    shape = gz.star(stroke_width=0.01, fill=(0, 0, 0.3, 0.7))
    shape = shape.rotate(-3.14159265358979 / 2.0)
    shape = shape.scale((im_size - 100) // 2)
    shape = shape.translate([im_size // 2, im_size // 2])

    # Some text to throw on the shape...
    txt = gz.text("Gizeh on pdf",
                  fontfamily="Arial",
                  fontsize=50,
                  fill=(0, 0, 0),
                  xy=(im_size // 2, im_size // 2))

    # Create pdf surface
    s = gz.PDFSurface("pdfsurface_test.pdf", im_size, im_size)
    # Draw shape on the PDF surface
    shape.draw(s)
    txt.draw(s)

    # Write file and close surface
    s.flush()
    s.finish()

    # Delete test PDF
    os.remove("pdfsurface_test.pdf")
Exemple #5
0
def test_pdfsurface(tmpdir):
    """Test PDFSurface class."""
    # 800x800 point image
    im_size = 800

    # Create a simple star shape with a fill
    shape = gz.star(stroke_width=0.01, fill=(0, 0, 0.3, 0.7))
    shape = shape.rotate(-3.14159265358979 / 2.0)
    shape = shape.scale((im_size - 100) // 2)
    shape = shape.translate([im_size // 2, im_size // 2])

    # Some text to throw on the shape...
    txt = gz.text("Gizeh on pdf",
                  fontfamily="Arial",
                  fontsize=50,
                  fill=(0, 0, 0),
                  xy=(im_size // 2, im_size // 2))

    # Create pdf surface
    filepath = os.path.join(str(tmpdir), "pdfsurface_test.pdf")
    s = gz.PDFSurface(filepath, im_size, im_size)
    # Draw shape on the PDF surface
    shape.draw(s)
    txt.draw(s)

    # Write file and close surface
    s.flush()
    s.finish()
def draw_stars(t):
    surface = gz.Surface(640, 120, bg_color=WHITE_GIZEH)
    for i in range(5):
        star = gz.star(
            nbranches=5, radius=120*0.2, ratio=0.5,
            xy=[100*(i+1), 50], fill=GREEN, angle=t * pi)
        star.draw(surface)
    return surface.get_npimage()
Exemple #7
0
def star(color, position):
    real_position = [position[0] + 0, position[1] + 1]
    return gizeh.star(nbranches=5,
                      fill=color,
                      xy=real_position,
                      radius=20,
                      ratio=0.5,
                      angle=0.95,
                      stroke_width=2)
Exemple #8
0
def draw_stars(t):
    surface = gz.Surface(640, 120, bg_color=(1, 1, 1))
    for i in range(5):
        star = gz.star(nbranches=5,
                       radius=120 * 0.2,
                       xy=[100 * (i + 1), 50],
                       fill=(0, 1, 0),
                       angle=t * pi)
        star.draw(surface)
    return surface.get_npimage()
Exemple #9
0
    def draw(self, ind):
        omega = 2 * math.pi / self.period
        angle = self.angle + omega * float(ind)

        self.kwargs['nbranches'] = self.nbranches
        self.kwargs['angle'] = angle

        star = gizeh.star(
            radius=self.radius[ind],
            xy=self.center,
            fill=self.color[ind],
            **self.kwargs
        )
        star.draw(self.surface)
    def draw(self, ind):
        super().draw(ind)

        for i in range(self.multiplicity):
            j = i + 1
            if ind > j:
                radius = self.radius[ind - j]

                if radius >= self.bounce_sensitive_radius * max(self.radius):
                    form = gizeh.star(radius=radius * (self.radius_add + j) *
                                      self.radius_factor,
                                      xy=self.center,
                                      fill=None,
                                      stroke=self.color[ind - j],
                                      stroke_width=self.stroke_width(j),
                                      **self.kwargs)
                    form.draw(self.surface)
def eval_genomes(genomes, config):
    global generation
    generation += 1
    best_genome = None
    best_fitness = 0

    for genome_id, genome in genomes:
        genome.fitness = eval_genome(genome, config)

        if genome.fitness > best_fitness:
            best_genome = genome
            best_fitness = genome.fitness

    # visualization for best genome
    if visualize:
        net = neat.nn.FeedForwardNetwork.create(best_genome, config)
        sim = cart_pole.CartPole(**initial_values)

        while sim.t < simulation_seconds:
            inputs = sim.get_scaled_state()
            action = net.activate(inputs)

            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            if abs(sim.x) >= sim.position_limit or abs(
                    sim.theta) >= sim.angle_limit_radians:
                break

            cart = gz.rectangle(lx=25 * scale,
                                ly=12.5 * scale,
                                xy=(150 * scale, 80 * scale),
                                fill=(0, 1, 0))

            force_direction = 1 if force > 0 else -1

            force_rect = gz.rectangle(lx=5,
                                      ly=12.5 * scale,
                                      xy=(150 * scale - force_direction *
                                          (25 * scale) / 2, 80 * scale),
                                      fill=(1, 0, 0))

            cart_group = gz.Group([cart, force_rect])

            star = gz.star(radius=10 * scale,
                           fill=(1, 1, 0),
                           xy=(150 * scale, 25 * scale),
                           angle=-math.pi / 2)

            pole = gz.rectangle(lx=2.5 * scale,
                                ly=50 * scale,
                                xy=(150 * scale, 55 * scale),
                                fill=(1, 1, 0))

            pole_group = gz.Group([pole, star])

            # convert position to display units
            visX = sim.x * 50 * scale

            # draw background
            surface = gz.Surface(w, h, bg_color=(0, 0, 0))

            # draw cart, pole and text
            group = gz.Group([
                cart_group.translate((visX, 0)),
                pole_group.translate(
                    (visX, 0)).rotate(sim.theta,
                                      center=(150 * scale + visX, 80 * scale)),
                gz.text('Gen %d Time %.2f (Fitness %.2f)' %
                        (generation, sim.t, best_genome.fitness),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 25),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('x: %.2f' % (sim.x, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 50),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('dx: %.2f' % (sim.dx, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 75),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('theta: %d' % (sim.theta * 180 / math.pi, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 100),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('dtheta: %d' % (sim.dtheta * 180 / math.pi, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 125),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('force: %d' % (force, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 0, 0),
                        xy=(10, 150),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
            ])
            group.draw(surface)

            img = cv2.UMat(surface.get_npimage())
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

            cv2.imshow('result', img)
            if cv2.waitKey(1) == ord('q'):
                exit()
Exemple #12
0
    force_direction = 1 if force > 0 else -1

    force_rect = gz.rectangle(
        lx=5,
        ly=12.5 * scale,
        xy=(150 * scale - force_direction * (25 * scale) / 2, 80 * scale),
        fill=(1, 0, 0)
    )

    cart_group = gz.Group([
        cart,
        force_rect
    ])

    star = gz.star(radius=10 * scale, fill=(1, 1, 0), xy=(150 * scale, 25 * scale), angle=-math.pi / 2)

    pole = gz.rectangle(
        lx=2.5 * scale,
        ly=50 * scale,
        xy=(150 * scale, 55 * scale),
        fill=(1, 1, 0)
    )

    pole_group = gz.Group([
        pole,
        star
    ])

    # convert position to display units
    visX = sim.x * 50 * scale
Exemple #13
0
import gizeh as gz
import numpy as np

surface = gz.Surface(200, 200, bg_color=(1, 0.9, 0.6))

star1 = gz.star(radius=70, ratio=0.4, fill=(1, 1, 1), angle=-np.pi / 2, stroke_width=2, stroke=(1, 0, 0))
star2 = gz.star(radius=55, ratio=0.4, fill=(1, 0, 0), angle=-np.pi / 2)
stars = gz.Group([star1, star2]).translate([100, 100])
stars.draw(surface)

surface.write_to_png("star.png")
Exemple #14
0
import gizeh as gz
import numpy as np

surface = gz.Surface(200,200, bg_color=(1, 0.9, 0.6))

star1 = gz.star(radius=70, ratio=.4, fill=(1,1,1), angle=-np.pi/2,
             stroke_width=2, stroke=(1,0,0))
star2 = gz.star(radius =55, ratio=.4, fill=(1,0,0), angle=-np.pi/2)
stars = gz.Group([ star1, star2 ]).translate([100,100])
stars.draw(surface)

surface.write_to_png("star.png")