diff options
| author | Lex Neva <github.com@lexneva.name> | 2019-03-19 23:28:19 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2019-03-19 23:28:19 -0400 |
| commit | 685df3b3f05f8e81751bde2e7bb59c50b658f764 (patch) | |
| tree | db2a924c71e11c226f70de577a2607c66335965c /lib/stitches/auto_fill.py | |
| parent | 68590492f55af51bf681a73200f12eb4d9a4f27a (diff) | |
fix start/end at top or bottom of shape
Diffstat (limited to 'lib/stitches/auto_fill.py')
| -rw-r--r-- | lib/stitches/auto_fill.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/stitches/auto_fill.py b/lib/stitches/auto_fill.py index 3dff3d96..b85d335e 100644 --- a/lib/stitches/auto_fill.py +++ b/lib/stitches/auto_fill.py @@ -59,7 +59,7 @@ def auto_fill(shape, fill_stitch_graph = build_fill_stitch_graph(shape, angle, row_spacing, end_row_spacing) check_graph(fill_stitch_graph, shape, max_stitch_length) travel_graph = build_travel_graph(fill_stitch_graph, shape, angle, underpath) - path = find_stitch_path(fill_stitch_graph, starting_point, ending_point) + path = find_stitch_path(fill_stitch_graph, travel_graph, starting_point, ending_point) result = path_to_stitches(path, travel_graph, fill_stitch_graph, angle, row_spacing, max_stitch_length, running_stitch_length, staggers, skip_last) @@ -281,14 +281,14 @@ def check_graph(graph, shape, max_stitch_length): "This most often happens because your shape is made up of multiple sections that aren't connected.")) -def nearest_node_on_outline(graph, point): +def nearest_node(graph, point): point = shgeo.Point(*point) nearest = min(graph.nodes, key=lambda node: shgeo.Point(*node).distance(point)) return nearest -def find_stitch_path(graph, starting_point=None, ending_point=None): +def find_stitch_path(graph, travel_graph, starting_point=None, ending_point=None): """find a path that visits every grating segment exactly once Theoretically, we just need to find an Eulerian Path in the graph. @@ -315,12 +315,13 @@ def find_stitch_path(graph, starting_point=None, ending_point=None): if starting_point is None: starting_point = graph.nodes.keys()[0] - starting_node = nearest_node_on_outline(graph, starting_point) + starting_node = nearest_node(graph, starting_point) if ending_point is None: + ending_point = starting_point ending_node = starting_node else: - ending_node = nearest_node_on_outline(graph, ending_point) + ending_node = nearest_node(graph, ending_point) # The algorithm below is adapted from networkx.eulerian_circuit(). path = [] @@ -354,6 +355,14 @@ def find_stitch_path(graph, starting_point=None, ending_point=None): if starting_node is not ending_node: path.insert(0, PathEdge((starting_node, ending_node), key="initial")) + real_start = nearest_node(travel_graph, starting_point) + if real_start != starting_node: + path.insert(0, PathEdge((real_start, starting_node), key="outline")) + + real_end = nearest_node(travel_graph, ending_point) + if real_end != ending_node: + path.append(PathEdge((ending_node, real_end), key="outline")) + return path |
