Example #1
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()
Example #2
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()
Example #3
0
    def save_svg(self):
        svg = vector.SVG(folder="cv12", name="maze_%d" % self.size)

        hex_height = self.seg_size * math.sin(math.radians(60))

        for cell in self.maze.values():
            points = []
            if cell.x % 2 == 0:
                for i in range(6):
                    points.append(
                        (self.seg_size * math.cos(2 * math.pi * i / 6) +
                         self.seg_size + (1.5 * cell.x * self.seg_size),
                         self.seg_size * math.sin(2 * math.pi * i / 6) +
                         hex_height + 2 * hex_height * cell.y))
            else:
                for i in range(6):
                    points.append(
                        (self.seg_size * math.cos(2 * math.pi * i / 6) +
                         self.seg_size + (1.5 * cell.x * self.seg_size),
                         self.seg_size * math.sin(2 * math.pi * i / 6) +
                         hex_height + hex_height + (2 * cell.y * hex_height)))

            if cell.walls["right-lower"]:
                svg.add_line(points[0][0], points[0][1], points[1][0],
                             points[1][1])
            if cell.walls["bottom"]:
                svg.add_line(points[1][0], points[1][1], points[2][0],
                             points[2][1])
            if cell.walls["left-lower"]:
                svg.add_line(points[2][0], points[2][1], points[3][0],
                             points[3][1])
            if cell.walls["left-upper"]:
                svg.add_line(points[3][0], points[3][1], points[4][0],
                             points[4][1])
            if cell.walls["top"]:
                svg.add_line(points[4][0], points[4][1], points[5][0],
                             points[5][1])
            if cell.walls["right-upper"]:
                svg.add_line(points[5][0], points[5][1], points[0][0],
                             points[0][1])

            if cell.x == 0 and cell.y == 0:
                svg.objects.append(
                    vector.Point(points[0][0] - self.seg_size,
                                 points[0][1],
                                 color="green"))
            if cell.x == self.size - 1 and cell.y == self.size - 1:
                svg.objects.append(
                    vector.Point(points[0][0] - self.seg_size,
                                 points[0][1],
                                 color="red"))

        svg.save()
Example #4
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
Example #5
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()
Example #6
0
def get_intersection(line1, line2):
    # https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line
    x1 = line1.start.x; y1 = line1.start.y
    x2 = line1.end.x; y2 = line1.end.y
    x3 = line2.start.x; y3 = line2.start.y
    x4 = line2.end.x; y4 = line2.end.y

    # no intersection
    if (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) == 0:
        return None

    p1 = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
    p2 = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))

    p = vector.Point(p1, p2)

    # is our intersection on both edges?
    if line1.is_in(p) and line2.is_in(p):
        # two ends touchig --> no intersection in this case
        if (p == line1.start or p == line1.end) and (p == line2.start or p == line2.end):
            return None
        else:
            return p
    else:
        return None
Example #7
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
Example #8
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()
Example #9
0
from src import board

WIN_WIDTH = 700
WIN_HEIGHT = 1000

window = window.Window(width=WIN_WIDTH,
                       height=WIN_HEIGHT,
                       title="Werewolves versus Vampires")
window.color = pygame.Color('yellow')
board_decorator: game_object.GameObject = game_object.GameObject(
    (0, 300), (WIN_WIDTH, WIN_HEIGHT - 300))
board_decorator.sprite = graphics.GraphicResource(
    pathlib.Path('resources/board_decorator.png'),
    lib.vector.Size(700, 700)), lib.vector.Size(700, 700)

board = board.create_board(position=vector.Point(75, 75),
                           size=vector.Size(WIN_WIDTH - 150, WIN_HEIGHT - 450),
                           columns=10,
                           rows=10,
                           tile_size=vector.Size(55, 55))
# board.color = (0, 255, 255)

board_decorator.children.append(board)
window.objects.append(board_decorator)

event_handler = events.EventHandler()
event_handler.events.append(events.ExitEvent())
click_event = src.events.MouseButtonDownEvent(board, vector.Point(0, 300))
click_event.draw_function = window.draw_window
event_handler.events.append(click_event)
while True: