Esempio n. 1
0
def init_points(positions):
    points = [None] * len(positions)

    for vertex, position in positions.items():
        point = GraphicsPoint(*position, False)
        point.index = vertex
        points[vertex] = point

    return points
Esempio n. 2
0
def convert_to_game_structure(graph: nx.Graph, embedding: nx.PlanarEmbedding,
                              path_grouping):
    positions = scale_positions(
        nx.combinatorial_embedding_to_pos(embedding, True))
    points = [GraphicsPoint] * len(graph.nodes)
    paths = []

    # Initialize points
    for index, position in positions.items():
        point = GraphicsPoint(*position, False)
        point.index = index
        points[index] = point

    # Initialize paths
    for path_start, path_end, mid_point in path_grouping:
        start_point = points[path_start[0]]
        end_point = points[path_end[1]]

        # The path was created from the same point to itself
        if start_point == end_point:
            end_point = points[path_start[1]]

            # Make small difference to width of the paths
            delta_pos = end_point - start_point
            mid_point = start_point + delta_pos.scalar(0.5)
            shift = delta_pos.rotate_anticlock().normalized().scalar(5)

            pos_control = mid_point + shift
            neg_control = mid_point - shift

            path_0 = Path([
                GraphicsBezier(start_point, end_point, gc.LINE_COLOR,
                               pos_control, pos_control)
            ], start_point, end_point)
            path_1 = Path([
                GraphicsBezier(start_point, end_point, gc.LINE_COLOR,
                               neg_control, neg_control)
            ], start_point, end_point)

            # Update paths of points
            start_point.add_to_path(path_0)
            start_point.add_to_path(path_1)
            end_point.add_to_path(path_0)
            end_point.add_to_path(path_1)

            paths.append((path_0, path_1, points[path_start[1]]))
        # Normal path
        else:
            middle_point = points[path_start[1]]

            path_0 = Path([GraphicsBezier(start_point, middle_point)],
                          start_point, middle_point)
            path_1 = Path([GraphicsBezier(middle_point, end_point)],
                          middle_point, end_point)

            # Update paths of points
            start_point.add_to_path(path_0)
            middle_point.add_to_path(path_0)
            middle_point.add_to_path(path_1)
            end_point.add_to_path(path_1)

            paths.append((path_0, path_1, middle_point))

        # Test whether the points are still valid
        valid_point(start_point, start_point, end_point)
        valid_point(end_point, start_point, end_point)
    return points, paths