Ejemplo n.º 1
0
def draw_lines_and_intersects():
    img = SVGImage(IMG_WIDTH, IMG_HEIGHT, center_origin=False, show_borders_and_origin=False, animate=False, animation_speed=ANIMATION_SPEED)
    lines = []

    ## draw lines
    for i in range(LINE_COUNT):
        line = get_random_line(LINE_LENGTH)
        lines.append(line)
        img.add_line(*line, width=LINE_WIDTH)

    ## draw intersections
    for line in lines:
        for i in lines:
            if line == i:
                continue
            if intersect(line, i):
                px, py = get_intersect(line, i)
                img.add_circle(px, py, INTERSECT_RADIUS, color="black", fill="red")

    img.save("img/line_intersections.svg")
Ejemplo n.º 2
0
def draw_maze(filename,
              maze_side,
              missing_walls,
              show_start_end=True,
              animate=False,
              img_size=500,
              line_width=2):
    """ Draws maze into svg image.
    Checks if there should not be missing wall in @missing_walls.
    """
    size = img_size
    width = line_width
    img = SVGImage(size,
                   size,
                   center_origin=False,
                   show_borders_and_origin=False,
                   animate=animate,
                   animation_speed=0.05)

    # draw borders
    img.add_line(0, 0, 0, size, width=2 * width)
    img.add_line(0, size, size, size, width=2 * width)
    img.add_line(size, size, size, 0, width=2 * width)
    img.add_line(size, 0, 0, 0, width=2 * width)

    # draw start and end
    # start is always upper-top and end always bottom-right
    if show_start_end:
        img.add_circle(size / maze_side / 2,
                       size / maze_side / 2,
                       size / maze_side / 4,
                       fill="green")
        img.add_circle(maze_side * (size / maze_side) - (size / maze_side / 2),
                       maze_side * (size / maze_side) - (size / maze_side / 2),
                       size / maze_side / 4,
                       fill="red")

    # draw vertical walls
    for row in range(maze_side):
        for col in range(maze_side - 1):
            current_wall = ((row, col), (row, col + 1))
            if current_wall in missing_walls or current_wall[::
                                                             -1] in missing_walls:
                continue
            x0 = (col + 1) * (size / maze_side)
            y0 = row * (size / maze_side)
            x1 = x0
            y1 = (row + 1) * (size / maze_side)
            img.add_line(x0, y0, x1, y1, width=width)

    # draw horizontal walls
    for row in range(maze_side - 1):
        for col in range(maze_side):
            current_wall = ((row, col), (row + 1, col))
            if current_wall in missing_walls or current_wall[::
                                                             -1] in missing_walls:
                continue
            x0 = col * (size / maze_side)
            y0 = (row + 1) * (size / maze_side)
            x1 = (col + 1) * (size / maze_side)
            y1 = y0
            img.add_line(x0, y0, x1, y1, width=width)
    img.save(filename)
Ejemplo n.º 3
0
def draw_triangulation():
    img = SVGImage(IMG_WIDTH,
                   IMG_HEIGHT,
                   center_origin=False,
                   show_borders_and_origin=True,
                   animate=True,
                   animation_speed=ANIMATION_SPEED)

    def generate_points(number):
        points = []
        for i in range(number):
            x = get_random_number(BUFFER, IMG_WIDTH - BUFFER)
            y = get_random_number(BUFFER, IMG_HEIGHT - BUFFER)
            points.append((x, y))
        return points

    points = generate_points(POINTS_NUM)
    #points = [(200, 200), (400, 150), (500, 400), (450, 700), (300, 600)]
    #points = [(200, 200), (400, 150), (500, 400), (450, 700)]
    #points = [(341, 518), (653, 605), (363, 476), (676, 308)]

    print(points)

    def shorten_line(line, step=10):
        x1, y1, x2, y2 = list(line)
        if x2 > x1:
            x = x2 - x1
            x1 += x / step
            x2 -= x / step
        else:
            x = x1 - x2
            x1 -= x / step
            x2 += x / step
        if y2 > y1:
            y = y2 - y1
            y1 += y / step
            y2 -= y / step
        else:
            y = y1 - y2
            y1 -= y / step
            y2 += y / step
        return (x1, y1, x2, y2)

    def intersect_some_collected(line, collected):
        for i in collected:
            a = shorten_line(i)
            b = shorten_line(line)

            if intersect(a, b):
                print("III", line, i)
                return True
        return False

    def get_lines(points):
        collected = []
        for point1 in points:
            for point2 in points:
                if point1 == point2:
                    continue
                if point1 + point2 in collected:
                    continue
                if point2 + point1 in collected:
                    continue
                line = point1 + point2

                if not intersect_some_collected(line, collected):
                    collected.append(line)
                print(collected)
        return collected

    lines = get_lines(points)
    """
    lines = [ (200, 200, 450, 700)]
    x = (300, 600, 400, 150)
    print(intersect_some_collected(x, lines))
    print(intersect(lines[0], x))
    img.add_line(*x, width=LINE_WIDTH*4)
    img.add_circle(330.7692307692308, 461.53846153846155, POINTS_RADIUS, fill="red")
    """

    ## draw points
    for point in points:
        img.add_circle(point[0],
                       point[1],
                       POINTS_RADIUS,
                       color="black",
                       fill="red")

    ## draw lines
    for line in lines:
        img.add_line(*line, width=LINE_WIDTH)

    img.save("img/triangulation_animate.svg")