Exemple #1
0
    def test_canvas_shapes4(self):

        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok([Point(5, 5), Point(5, 8), Point(8, 9), Point(7, 5)])
        b1 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_b1 = PolygonShape(b1.points, style=Style(color='blue'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                box = cv.get_box_for_cell(r, c)
                if r == 3 and c == 3:
                    cv = Canvas.add_shape2(cv,
                                           shape_b1,
                                           box,
                                           label=f"({str(r)},{str(c)})")
                else:
                    cv = Canvas.add_shape2(cv,
                                           shape_c1,
                                           box,
                                           label=f"({str(r)},{str(c)})")

        with open('canvas4.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #2
0
def runn(name, basic, num_levels, svg_file, is_symmetric=False):
    start_time = time.time()
    b0 = Blok(basic)
    b1 = Blok.rotate(b0, Blok.get_edge(b0, 0).start_pt, math.pi / 4)
    prev_level = set()
    prev_level.add(b0)
    all = [(1, b0)]

    for lev in range(num_levels - 1):
        cache = levels_cache.get((name, lev), None)
        if cache:
            curr_level = cache
        else:
            curr_level = gen_next_level(prev_level, b1, is_symmetric)
            levels_cache[(name, lev)] = curr_level
        for s in iter(curr_level):
            all.append((lev + 2, s))
        prev_level = curr_level

    normalized_bloks = [(n, Blok.normalize(b)) for n, b in all]
    bigbox = Box.bounding_box_of_boxex(
        [PolygonShape(b.points).bounding_box() for _, b in normalized_bloks])

    d = int(math.sqrt(len(all)))
    cv = Canvas(width=20, height=20, nrows=d + 2, ncols=d + 2)
    i = 0
    for n, b in normalized_bloks:
        c = int(i / d)
        r = int(i % d)
        box = cv.get_box_for_cell(r, c)
        sh = PolygonShape(b.points, style=Style(color='black', size='0.005'))
        if b.component_blocks:
            component_polygons = [
                PolygonShape(bp.points, style=Style(color='red'))
                for bp in b.component_blocks
            ] + [sh]
            sh = CompositeShape(component_polygons)
            cv = Canvas.add_shape3(cv,
                                   sh,
                                   box,
                                   bigbox,
                                   margin_percent=0.3,
                                   label=f"{str(n)}")
        i = i + 1

    with open(svg_file, 'w') as fd:
        Canvas.render_as_svg(cv, file=fd)
    print(
        f"{name}: Num shapes: {len(all)} --- Time: {(time.time() - start_time)} seconds ---"
    )
Exemple #3
0
    def test_canvas_shapes3(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                box = cv.get_box_for_cell(r, c)
                cv = Canvas.add_shape2(cv, shape_c1, box)

        with open('canvas3.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #4
0
    def test_canvas_shapes(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)
        c1 = Canvas(width=20, height=20)
        shape_b0 = PolygonShape(b0.points)
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))
        bbox = shape_nb0.bounding_box()
        path = bbox.get_path()
        shape_bb = PolygonShape(path, style=Style(color='blue'))

        c2 = Canvas.add_shape(c1, shape_b0)
        c3 = Canvas.add_shape(c2, shape_nb0)
        c4 = Canvas.add_shape(c3, shape_bb)

        box = Box(Point(10, 0), Point(20, 10))
        c5 = Canvas.add_shape2(
            c4, PolygonShape(nb0.points, style=Style(color='green')), box)
        box2 = Box(Point(10, 10), Point(20, 20))
        c6 = Canvas.add_shape2(
            c5, PolygonShape(nb0.points, style=Style(color='yellow')), box2)

        with open('canvas1.svg', 'w') as fd:
            Canvas.render_as_svg(c6, file=fd)
Exemple #5
0
    def test_canvas_shapes2(self):
        b0 = Blok([Point(10, 11), Point(12, 13), Point(13, 11)])
        nb0 = Blok.normalize(b0)

        c1 = Canvas(width=20, height=20)
        shape_b0 = PolygonShape(b0.points, style=Style(color='green'))
        shape_nb0 = PolygonShape(nb0.points, style=Style(color='red'))

        shape_c1 = CompositeShape([shape_b0, shape_nb0])

        c1 = Canvas.add_shape(c1, shape_c1)

        box = Box(Point(10, 0), Point(20, 10))

        c2 = Canvas.add_shape2(c1, shape_c1, box)
        c3 = Canvas.add_shape2(c2, shape_c1, Box(Point(10, 10), Point(12, 12)))

        with open('canvas2.1.svg', 'w') as fd:
            Canvas.render_as_svg(c3, file=fd)
Exemple #6
0
    def test_simple_generator(self):
        b0 = Blok(SQUARE)
        b1 = Blok(SQUARE)
        cv = Canvas(width=20, height=20, nrows=4, ncols=4)

        for r in range(4):
            for c in range(4):
                (_, b2) = Blok.align_blocks_on_edge(b0, r, b1, c)
                b0_shape = PolygonShape(b0.points, style=Style(color='blue'))
                b2_shape = PolygonShape(b2.points, style=Style(color='red'))
                b0_b2_shape = CompositeShape([b0_shape, b2_shape])
                box = cv.get_box_for_cell(r, c)
                cv = Canvas.add_shape2(cv,
                                       b0_b2_shape,
                                       box,
                                       label=f"({str(r)},{str(c)})")

        with open('canvas5.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)
Exemple #7
0
    def test_gen2(self):
        b0 = Blok(SQUARE)
        b1 = Blok.rotate(b0, Blok.get_edge(b0, 0).start_pt, math.pi / 4)
        uniq = set()
        uniq.add(b0)
        all = []
        level1 = self.gen_next_level(uniq, b1)

        print("level1")
        for s in iter(level1):
            all.append(s)

        print("level2")
        level2 = self.gen_next_level(level1, b1)
        for s in iter(level2):
            all.append(s)

        print("level3")
        level3 = self.gen_next_level(level2, b1)
        for s in iter(level3):
            all.append(s)

        normalized_bloks = [Blok.normalize(b) for b in all]
        bigbox = Box.bounding_box_of_boxex(
            [PolygonShape(b.points).bounding_box() for b in normalized_bloks])

        d = int(math.sqrt(len(all)))
        cv = Canvas(width=20, height=20, nrows=d, ncols=d)
        i = 0
        for b in normalized_bloks:
            print(b)
            c = int(i / d)
            r = int(i % d)
            sh = PolygonShape(b.points, style=Style(color='black'))
            box = cv.get_box_for_cell(r, c)
            cv = Canvas.add_shape2(cv, sh, box, label=f"({str(r)},{str(c)})")
            i = i + 1

        with open('all1.svg', 'w') as fd:
            Canvas.render_as_svg(cv, file=fd)