예제 #1
0
    def test_path_intersect_no_mirror_2(self):
        anchor_points_0 = [
            GraphicsPoint(100, 100),
            Point(473.7132417184837, 208.4978496002687),
            GraphicsPoint(457, 125)
        ]

        anchor_points_1 = [
            GraphicsPoint(500, 400),
            Point(573, 371),
            Point(550, 177),
            Point(586, 146),
            Point(337, 129),
            Point(536, 22),
            Point(689, 67),
            Point(728, 344),
            Point(650, 472)
        ]

        p_start = GraphicsPoint(100, 100)
        p_end = GraphicsPoint(457, 125)

        path = Path([
            GraphicsBezier(p_start, p_end, Point(278.5, 112.5),
                           Point(404.8987230708711, 188.92401096739675))
        ], p_start, p_end)
        path2 = Path.from_points(anchor_points_1)

        intersections = path.intersects(path2)
        intersections_2 = path2.intersects(path)

        show_paths_2([path, path2], intersections + intersections_2)
        self.assertEqual(len(intersections), len(intersections_2))
예제 #2
0
    def point_touches_path(self, point: GraphicsPoint) -> bool:
        approximation = self.approximate()

        if point.equals(self.start_point) or point.equals(self.end_point):
            return False

        for i in range(len(approximation) - 1):
            segment_start = approximation[i]
            segment_end = approximation[i + 1]

            if segment_start.distance_sq(
                    self.start_point) < 81 and point.distance_sq(
                        self.start_point) < 81:
                continue
            if segment_end.distance_sq(
                    self.end_point) < 81 and point.distance_sq(
                        self.end_point) < 81:
                continue

            segment = Segment(segment_start, segment_end)

            if segment.over_segment(point):
                if abs(segment.dist(point)) < point.radius:
                    return True
        return False
예제 #3
0
    def test_path_no_intersect_2(self):
        # anchor_points_0 = [GraphicsPoint(450,100), Point(499.27232558764285,264.34980738137415), Point(520.9894097054131,309.3872169141358), Point(492.6055772197633,350.5497976571671), GraphicsPoint(500,400)]
        anchor_points_1 = [
            GraphicsPoint(100, 100),
            GraphicsPoint(350.0, 200.0),
            GraphicsPoint(600, 300)
        ]
        anchor_points_0 = [
            GraphicsPoint(450, 100),
            Point(289.01257267912837, 295.27070180414165),
            Point(328.57076133309056, 325.8509194360934),
            Point(377.21153620107134, 337.4299962664114),
            Point(427.0452569066222, 333.35565206873423),
            Point(468.7047058662693, 361.0050700079705),
            GraphicsPoint(500, 400)
        ]
        (start_path_0, end_path_0,
         mid_point_1) = add_path(anchor_points_0[0], anchor_points_0[-1],
                                 anchor_points_0)
        (start_path_1, end_path_1,
         mid_point_1) = add_path(anchor_points_1[0], anchor_points_1[-1],
                                 anchor_points_1)

        intersections = start_path_0.intersects(
            start_path_1) + start_path_0.intersects(
                end_path_1) + end_path_0.intersects(
                    start_path_1) + end_path_0.intersects(end_path_1)

        show_paths_2([start_path_0, end_path_0, start_path_1, end_path_1],
                     intersections)
        self.assertEqual(True, len(intersections) > 0)
예제 #4
0
    def generate_starting_points(self):
        start_points = []
        GraphicsPoint.setLastID(0)
        for i in range(self.NUM_POINTS):
            start_points.append(
                GraphicsPoint(random.randint(50, gc.WINDOW_WIDTH - 50),
                              random.randint(50, gc.WINDOW_HEIGHT - 50)))

        return start_points
예제 #5
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
예제 #6
0
    def set_initial_state(self, initial_points, initial_paths):
        self.base_region = Region(set(initial_points),
                                  self.border_points,
                                  edge_map={},
                                  exclusions=set(),
                                  surf=self.SCREEN)

        for start_path, end_path, mid_point in initial_paths:
            current_region = self.base_region.find_region(mid_point)
            # Add the paths to the region
            current_region.update_region_tree([start_path, end_path],
                                              mid_point)

            self.paths.append(start_path)
            self.paths.append(end_path)

        self.points = set(initial_points)
        GraphicsPoint.setLastID(len(self.points))
예제 #7
0
    def points_to_graphicspoints(self, points, color=gc.POINT_COLOR, radius=9):
        lst = []
        for p in points:
            gp = GraphicsPoint.from_point(p, False)

            gp.update_color(color)
            gp.update_radius(radius)

            lst.append(gp)
        return lst
예제 #8
0
    def test_path_intersect_5(self):
        anchor_points_0 = [
            GraphicsPoint(403.01382054213786, 117.09675232181752),
            GraphicsPoint(450, 100)
        ]

        anchor_points_1 = [
            GraphicsPoint(422.05639822420903, 417.833171793951),
            GraphicsPoint(450.55461679819734, 407.4265792951324),
            GraphicsPoint(500, 400)
        ]

        path = Path.from_points(anchor_points_0)

        path2 = Path.from_points(anchor_points_1)

        intersections = path.intersects(path2)

        show_paths_2([path, path2], intersections)
        self.assertEqual(False, len(intersections) > 0)
예제 #9
0
    def test_path_intersect_4(self):
        anchor_points_0 = [
            GraphicsPoint(440.3994196807172, 149.06963274300102),
            GraphicsPoint(450, 100)
        ]

        anchor_points_1 = [
            GraphicsPoint(450, 100),
            GraphicsPoint(402.0865680692103, 419.70860894843224),
            GraphicsPoint(422.05639822420903, 417.833171793951)
        ]

        path = Path.from_points(anchor_points_0)

        path2 = Path.from_points(anchor_points_1)

        intersections = path.intersects(path2)

        show_paths_2([path, path2], intersections)
        self.assertEqual(False, len(intersections) > 0)
예제 #10
0
    def test_path_intersect_3(self):
        anchor_points_0 = [
            GraphicsPoint(100, 100),
            Point(376, 61),
            Point(639, 217),
            GraphicsPoint(546, 357)
        ]

        anchor_points_1 = [
            GraphicsPoint(450, 100),
            GraphicsPoint(440.3994196807172, 149.06963274300102)
        ]

        path = Path.from_points(anchor_points_0)

        path2 = Path.from_points(anchor_points_1)

        intersections = path2.intersects(path)

        show_paths_2([path, path2], intersections)
        self.assertEqual(False, len(intersections) > 0)
예제 #11
0
 def key_event_handler(self, event):
     # Enter pressed
     if event.key == pygame.K_RETURN:
         new_point = GraphicsPoint(random.randrange(0, gc.WINDOW_WIDTH),
                                   random.randrange(0, gc.WINDOW_HEIGHT),
                                   True)
         self.base_region.add_point(new_point)
     # ESC pressed
     if event.key == pygame.K_ESCAPE:
         self.preview_path = None
         self.update_screen([])
         pygame.display.update()
         self.soft_reset()
예제 #12
0
    def test_path_self_no_intersect(self):
        start_end = GraphicsPoint(376, 95)
        anchor_points = [
            start_end,
            Point(154, 199),
            Point(302, 315),
            Point(493, 120), start_end
        ]

        path = Path.from_points(anchor_points)
        intersections = path.self_intersections()
        show_paths_2([path], intersections)
        self.assertEqual(0, len(intersections))
예제 #13
0
    def test_path_intersect_no_mirror(self):
        anchor_points_0 = [
            GraphicsPoint(450, 100),
            Point(473.7132417184837, 208.4978496002687),
            GraphicsPoint(466, 268)
        ]

        anchor_points_1 = [
            GraphicsPoint(100, 100),
            Point(230, 177),
            Point(548, 178),
            GraphicsPoint(701, 347)
        ]

        path = Path.from_points(anchor_points_0)
        path2 = Path.from_points(anchor_points_1)

        intersections = path.intersects(path2)
        intersections_2 = path2.intersects(path)

        show_paths_2([path, path2], intersections + intersections_2)
        self.assertEqual(len(intersections), len(intersections_2))
예제 #14
0
    def __init__(self, initial_points, initial_paths):
        pygame.init()
        pygame.display.set_caption("Sprouts")
        img = pygame.image.load("../../res/sproutsbg.png")
        pygame.display.set_icon(img)

        self.CLOCK = pygame.time.Clock()
        self.SCREEN = pygame.display.set_mode(
            (gc.WINDOW_WIDTH, gc.WINDOW_HEIGHT))
        self.SURFACE = pygame.Surface(self.SCREEN.get_size(), pygame.SRCALPHA)

        self.PLAYER = Player.PLAYER_1
        self.NUM_POINTS = 2

        # control variables
        self.creating_path = False
        self.preview_points = []
        self.preview_path = None
        self.paths = []

        self.suggest_points = []

        self.all_intersections = []
        self.SCREEN.blit(self.SURFACE, (0, 0))

        self.rrt = None

        self.border_points = [
            GraphicsPoint(0, 0, False),
            GraphicsPoint(gc.WINDOW_WIDTH, 0, False),
            GraphicsPoint(gc.WINDOW_WIDTH, gc.WINDOW_HEIGHT, False),
            GraphicsPoint(0, gc.WINDOW_HEIGHT, False)
        ]

        self.points = None
        self.base_region = None

        self.run(State.MAIN_MENU, [False])
예제 #15
0
    def hard_reset(self):
        self.PLAYER = Player.PLAYER_1

        self.creating_path = False

        self.preview_points = []
        self.preview_path = None
        self.paths = []

        self.suggest_points = []
        self.all_intersections = []
        self.SCREEN.blit(self.SURFACE, (0, 0))

        self.rrt = None

        self.border_points = [
            GraphicsPoint(0, 0, False),
            GraphicsPoint(gc.WINDOW_WIDTH, 0, False),
            GraphicsPoint(gc.WINDOW_WIDTH, gc.WINDOW_HEIGHT, False),
            GraphicsPoint(0, gc.WINDOW_HEIGHT, False)
        ]
        self.points = None
        self.base_region = None
예제 #16
0
def add_path(p_0: Point, p_1: Point, clicks: List[Point]):
    beziers = compute_graphics_beziers(clicks, gc.LINE_COLOR)

    bezier_count = len(beziers)
    mid_count = bezier_count // 2
    # If the amount of bezier curves is even
    if bezier_count % 2 == 0:
        mid = beziers[mid_count - 1].end_point
        mid_point = GraphicsPoint(mid.x, mid.y, True)

        start_beziers = beziers[0:mid_count]
        if start_beziers[-1].end_point.equals(mid_point):
            start_beziers[-1].end_point = mid_point
        end_beziers = beziers[mid_count:]
        if end_beziers[0].start_point.equals(mid_point):
            end_beziers[0].start_point = mid_point

        start_path = Path(start_beziers, p_0, mid_point)
        end_path = Path(end_beziers, mid_point, p_1)
    # If the amount of bezier curve is odd
    else:
        # Split bezier curvers into two paths and find middle point
        (bezier_first_end, bezier_second_start) = beziers[mid_count].split(0.5)
        mid = beziers[mid_count].evaluate(0.5)
        mid_point = GraphicsPoint(mid.x, mid.y, True)

        start_beziers = beziers[:mid_count]
        start_beziers.append(GraphicsBezier.from_bezier(bezier_first_end))
        if start_beziers[-1].end_point.equals(mid_point):
            start_beziers[-1].end_point = mid_point

        end_beziers = beziers[mid_count:]
        # end_beziers.insert(0, bezier_second_start)
        end_beziers[0] = GraphicsBezier.from_bezier(bezier_second_start)
        if end_beziers[0].start_point.equals(mid_point):
            end_beziers[0].start_point = mid_point
        start_path = Path(start_beziers, p_0, mid_point)
        end_path = Path(end_beziers, mid_point, p_1)

    # Add path to points
    p_0.add_to_path(start_path)
    p_1.add_to_path(end_path)
    mid_point.add_to_path(start_path)
    mid_point.add_to_path(end_path)

    return start_path, end_path, mid_point
예제 #17
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