def test_no_intersect(self): p0 = Point(300, 100) p1 = Point(300, 100) p2 = Point(350, 300) p3 = Point(500, 200) q0 = Point(300, 250) q1 = Point(500, 250) q2 = Point(450, 350) q3 = Point(150, 350) p = Bezier(p0, p3, p1, p2) q = Bezier(q0, q3, q1, q2) intersection = p.intersects(q) show_intersections(p, q, intersection) self.assertEqual(0, len(intersection))
def test_intersect_b(self): p0 = Point(200, 50) p1 = Point(300, 40) p2 = Point(360, 300) p3 = Point(400, 100) q0 = Point(350, 100) q1 = Point(200, 140) q2 = Point(340, 160) q3 = Point(360, 220) p = Bezier(p0, p3, p1, p2) q = Bezier(q0, q3, q1, q2) intersection = p.intersects(q) show_intersections(p, q, intersection) self.assertEqual(1, len(intersection))
def init_onion_graph(): positions = { 0: (0, 0), 1: (0, 30), 2: (0, -20), 3: (0, 10), 4: (0, 20), 5: (0, -10) } points = init_points(positions) paths = [ Path.from_points([points[0], Point(-10, 5), points[3]]), Path.from_points([points[0], Point(10, 5), points[3]]), Path.from_points([points[0], points[5]]), Path.from_points([points[5], points[2]]), Path.from_points([points[1], Point(-30, 0), points[2]]), Path.from_points([points[1], Point(30, 0), points[2]]), Path.from_points([points[1], points[4]]), Path.from_points([points[3], points[4]]), ] return points, init_edge_map(paths)
def test_error_intersection_2(self): p0 = Point(293.72241133583, 196.89385300691802) p1 = Point(280.23555111038667, 275.54739962389243) p2 = Point(267.174553773, 355.40299523856083) p3 = Point(304, 382) q0 = Point(384, 219) q1 = Point(329.4781702228842, 274.5823432565248) q2 = Point(302.4625676163959, 338.5497641105882) q3 = Point(274.95634044576843, 330.16468651304956) p = Bezier(p0, p3, p1, p2) q = Bezier(q0, q3, q1, q2) intersection = p.intersects(q) show_intersections(p, q, intersection) self.assertEqual(1, len(intersection))
def test_zclose_no_intersect_c(self): p0 = Point(200, 50) p1 = Point(400, 50) p2 = Point(350, 300) p3 = Point(400, 250) q0 = Point(280, 100) q1 = Point(200, 0) q2 = Point(350, 250) q3 = Point(360, 220) p = Bezier(p0, p3, p1, p2) q = Bezier(q0, q3, q1, q2) intersection = p.intersects(q) show_intersections(p, q, intersection) self.assertEqual(0, len(intersection))
def cubic_curve(start_p, end_p, control_p1, control_p2, t): t_sub = 1 - t t_sub_sq = t_sub * t_sub t_sq = t * t # Factors for the cubic curve start_scalar = t_sub * t_sub_sq end_scalar = t_sq * t control_1_scalar = 3 * t_sub_sq * t control_2_scalar = 3 * t_sub * t_sq res_x = start_p.x * start_scalar + end_scalar * end_p.x + control_1_scalar * control_p1.x \ + control_2_scalar * control_p2.x res_y = start_p.y * start_scalar + end_scalar * end_p.y + control_1_scalar * control_p1.y \ + control_2_scalar * control_p2.y return Point(res_x, res_y)
def test_straight_intersection_2(self): p0 = Point(425, 85) p1 = Point(500.77636499516825, 90.43288595352556) p2 = Point(512.5, 192.5) p3 = Point(600, 300) q0 = Point(638, 105) q3 = Point(427.67115932085926, 268.59592697198934) p = Bezier(p0, p3, p1, p2) q = Bezier(q0, q3) intersection = p.intersects(q) show_intersections(p, q, intersection) self.assertEqual(1, len(intersection))
def run_game(self, initial_points, initial_paths): self.set_initial_state(initial_points, initial_paths) last_mouse_pos = (0, 0) spacing = " " legend_text = "Esc: cancel path" + spacing + "Right Click: Path suggestion" + spacing + "Left Click: Draw path" + spacing + "Backspace: Main menu" legend = Label(0, 0, 800, 30, pygame.SRCALPHA, pygame.SRCALPHA, 0, legend_text, 18, gc.UI_BUTTON_TEXT_COLOR) player_1_label = Label(0, 0, 60, 30, gc.PLAYER_1_COLOR, gc.BLACK, 2, "Player 1", 20, gc.UI_BUTTON_TEXT_COLOR) player_2_label = Label(gc.WINDOW_WIDTH - 60, 0, 60, 30, gc.PLAYER_2_COLOR, gc.PLAYER_2_COLOR, 2, "Player 2", 20, gc.UI_BUTTON_TEXT_COLOR) surfaces = [] surfaces.extend( [legend.draw(), player_1_label.draw(), player_2_label.draw()]) if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) while True: current_mouse_pos = pygame.mouse.get_pos() surfaces.extend( [legend.draw(), player_1_label.draw(), player_2_label.draw()]) for event in pygame.event.get(): # exit via X in window corner if event.type == pygame.QUIT: sys.exit() # key pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: # self.SCREEN.fill(gc.BACKGROUND_COLOR) self.hard_reset() return (State.MAIN_MENU, []) self.key_event_handler(event) # mouse clicked if event.type == pygame.MOUSEBUTTONDOWN: mouse_point = Point(*event.pos) current_region = self.base_region.find_region( Point(*event.pos)) # p is a Point if collision exists, else None p = self.point_collision(current_region.game_points, event.pos) self.update_screen(surfaces) # right click = suggest a path (if point pressed is GraphicsPoint if event.button == 3 and p: self.suggest_points.append(p) if len(self.suggest_points) == 2: suggested_path_points = self.find_path() # User canceled if not suggested_path_points: self.soft_reset() break self.finalize_path(suggested_path_points) self.change_turn(player_1_label, player_2_label) # check for game over state if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) # reset control variables self.soft_reset() break elif event.button == 3: break # Cant draw straight lines by pressing twice in the same pixel if len(self.preview_points) > 0 and mouse_point.equals( self.preview_points[-1]): break # point collision and not drawing a path already if p and not self.creating_path: self.preview_path = None self.all_intersections = [] self.creating_path = True self.preview_points = [p] self.preview_path = Path.from_points( self.preview_points + [Point(*event.pos)]) # no point collision and creating path already elif not p and self.creating_path: self.preview_points.append(Point(*event.pos)) # point collision and creating path already elif p and self.creating_path: if p == self.preview_points[0] and len( self.preview_points) == 1: self.soft_reset() break self.preview_points.append(p) self.preview_path = Path.from_points( self.preview_points) (self.all_intersections, valid_path) = self.validate_path(self.preview_path) # path is not valid, show intersections if not valid_path: self.creating_path = False self.preview_path.change_color(gc.RED) self.all_intersections = self.points_to_graphicspoints( self.all_intersections, gc.BLACK, 5) self.soft_reset() break # Split path in two and calculate new point self.finalize_path(self.preview_points) self.change_turn(player_1_label, player_2_label) if self.game_over(self.base_region): self.update_screen([]) return (State.GAME_OVER, [initial_points, initial_paths]) self.soft_reset() if self.preview_points and self.creating_path: cur_point = Point(*pygame.mouse.get_pos()) self.preview_path.redraw(self.preview_points + [cur_point], self.get_color()) # Update the screen if the cursor has moved if last_mouse_pos != current_mouse_pos: self.SCREEN.fill(gc.BACKGROUND_COLOR) last_mouse_pos = current_mouse_pos self.update_screen(surfaces) pygame.display.update() surfaces = [] # controls fps self.CLOCK.tick(gc.FPS)
def self_cycle(self): points = [Point(100, 00), Point(600, 600)] region = Region()
def rand_free_conf_roi(self, roi): return Node( Point(random.randint(int(roi[0].x), int(roi[1].x)), random.randint(int(roi[0].y), int(roi[1].y))))
def rand_free_conf(self): return Node( Point(random.randint(-50, gc.WINDOW_WIDTH + 50), random.randint(-50, gc.WINDOW_HEIGHT + 50)))
def distance_control_points(self, curve) -> List[Point]: distance_points = [] for i in range(4): distance_points.append(Point(1 / 3 * i, self.dist(curve.points[i]))) return distance_points
def test_path_intersect_no_mirror_3(self): anchor_points_0 = [ Point(600, 300), Point(763, 232), Point(760, 248), Point(763.3812601455536, 279.9618353762123), Point(759.093503294539, 324.7570933974201), Point(752.2129530873079, 369.22796058983914), Point(728.8190694506658, 407.66916515467096) ] anchor_points_1 = [ Point(100, 100), Point(638, 170), Point(727, 212), Point(594, 357), Point(476, 177), Point(261, 207), Point(167.76045105799363, 197.0495733490862) ] 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))
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))
def lerp(start_p, end_p, t): diff_x = end_p.x - start_p.x diff_y = end_p.y - start_p.y return Point(start_p.x + diff_x * t, start_p.y + diff_y * t)