예제 #1
0
def square(a=100, cx=0, cy=0):
    """ Square with center in cx|cy and side size a """
    lines = [vector.Line(vector.Point(cx - a / 2, cy - a / 2), vector.Point(cx - a / 2, cy + a / 2)),
             vector.Line(vector.Point(cx - a / 2, cy + a / 2), vector.Point(cx + a / 2, cy + a / 2)),
             vector.Line(vector.Point(cx + a / 2, cy + a / 2), vector.Point(cx + a / 2, cy - a / 2)),
             vector.Line(vector.Point(cx + a / 2, cy - a / 2), vector.Point(cx - a / 2, cy - a / 2))]
    return lines
예제 #2
0
def convex_hull():
    svg = vector.SVG(folder="cv5", name="convex")

    points = []

    for i in range(100):
        point = vector.Point(random.uniform(100, 500),
                             random.uniform(100, 500))
        points.append(point)
        svg.objects.append(point)

    x_sorted = sorted(points, key=lambda point: point.x)

    bottom = []  # bottom half of the hull
    for point in x_sorted:
        while len(bottom) > 1 and orientation(bottom[-2], bottom[-1],
                                              point) <= 0:
            bottom.pop()
        bottom.append(point)

    top = []  # top half of the hull
    for point in reversed(x_sorted):
        while len(top) > 1 and orientation(top[-2], top[-1], point) <= 0:
            top.pop()
        top.append(point)

    convex = bottom + top
    for i in range(len(convex) - 1):
        svg.objects.append(vector.Line(convex[i], convex[i + 1]))

    svg.save()
예제 #3
0
def print_maze(index, maze_size, maze_str, positions, cell_size=50):
    # create a list of lists from give 'maze string'
    maze_iter = iter(maze_str)
    maze_list = [[next(maze_iter) for i in range(maze_size)] for j in range(maze_size)]

    svg = vector.SVG(folder="cv11", name="maze%d" % index)

    # add lines
    for i in range(maze_size + 1):
        svg.add_line(0 + cell_size*i, 0, 0 + cell_size*i, maze_size*cell_size)
        svg.add_line(0, 0 + cell_size*i, maze_size*cell_size, 0 + cell_size*i)

    # add start points and 'walls'
    for i in range(maze_size):
        for j in range(maze_size):
            if maze_list[i][j] == "#":
                rect = vector.Rectangle(x=i*cell_size, y=j*cell_size, size=cell_size, color="black")
                svg.objects.append(rect)
            elif maze_list[i][j] == "A":
                point = vector.Point(x=(i*cell_size + cell_size/2), y=(j*cell_size + cell_size/2), color="green")
                svg.objects.append(point)
            elif maze_list[i][j] == "B":
                point = vector.Point(x=(i*cell_size + cell_size/2), y=(j*cell_size + cell_size/2), color="red")
                svg.objects.append(point)

    # print moves
    for i in range(len(positions) - 1):
        start = vector.Point(x=(positions[i][0]*cell_size + cell_size/2), y=(positions[i][1]*cell_size + cell_size/2))
        end = vector.Point(x=(positions[i+1][0]*cell_size + cell_size/2), y=(positions[i+1][1]*cell_size + cell_size/2))
        line = vector.Line(start=start, end=end, color="blue")
        svg.objects.append(line)

    svg.save()
예제 #4
0
def intersections():
    lines = []
    svg = vector.SVG(folder="cv5", name="intersections")
    lenght = 500

    for i in range(50):
        a = vector.Point(random.uniform(500, 1000),
                         random.uniform(500, 1000))  # random start point
        rangle = math.radians(random.uniform(0, 360))  # random angle
        b = vector.Point(a.x + lenght * math.cos(rangle),
                         a.y + lenght * math.sin(rangle))

        line = vector.Line(a, b)
        lines.append(line)
        svg.objects.append(line)

    for line1 in lines:

        for line2 in lines:
            if line1 == line2:
                continue

            intersection = get_intersection(line1, line2)
            if intersection is not None:
                svg.objects.append(intersection)

    svg.save()
예제 #5
0
def do_it(matrix, lines):
    new_lines = []

    for line in lines:
        start_matrix = [[line.start.x], [line.start.y], [1]]
        start_res = dot(matrix, start_matrix)

        end_matrix = [[line.end.x], [line.end.y], [1]]
        end_res = dot(matrix, end_matrix)

        new_lines.append(vector.Line(vector.Point(float(start_res[0][0]), float(start_res[1][0])), vector.Point(float(end_res[0][0]), float(end_res[1][0]))))

    return new_lines
예제 #6
0
def triangulation():

    svg = vector.SVG(folder="cv5", name="triangulation")

    # random points
    points = []
    for i in range(50):
        point = vector.Point(random.uniform(100, 500), random.uniform(100, 500))
        points.append(point)
        svg.objects.append(point)

    # all lines
    lines = []
    for point1 in points:
        for point2 in points:
            if point1 == point2:
                continue
            lines.append(vector.Line(point1, point2))

    # sort lines by lenght
    lines = sorted(lines, key=lambda line: line.length)

    triang_lines = []
    for candidate in lines:
        add = True
        for line in triang_lines:
            if get_intersection(line, candidate):
                add = False  # intersection with some line in triangulation --> do not add
                break
        if add:
            triang_lines.append(candidate)

    for line in triang_lines:
        svg.objects.append(line)

    svg.save()