knit-graphs 0.0.6__py3-none-any.whl → 0.0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. knit_graphs-0.0.6.dist-info/licenses/LICENSE → LICENSE +21 -21
  2. README.md +75 -0
  3. docs/Makefile +20 -0
  4. docs/make.bat +35 -0
  5. docs/source/api/knit_graphs.artin_wale_braids.rst +58 -0
  6. docs/source/api/knit_graphs.rst +74 -0
  7. docs/source/conf.py +335 -0
  8. docs/source/index.rst +71 -0
  9. docs/source/installation.rst +67 -0
  10. knit_graphs/Course.py +156 -104
  11. knit_graphs/Knit_Graph.py +249 -186
  12. knit_graphs/Knit_Graph_Visualizer.py +680 -0
  13. knit_graphs/Loop.py +141 -155
  14. knit_graphs/Pull_Direction.py +68 -23
  15. knit_graphs/Yarn.py +424 -267
  16. knit_graphs/__init__.py +3 -3
  17. knit_graphs/_base_classes.py +173 -0
  18. knit_graphs/artin_wale_braids/Crossing_Direction.py +74 -15
  19. knit_graphs/artin_wale_braids/Loop_Braid_Graph.py +95 -62
  20. knit_graphs/artin_wale_braids/Wale.py +169 -93
  21. knit_graphs/artin_wale_braids/Wale_Braid.py +50 -30
  22. knit_graphs/artin_wale_braids/Wale_Braid_Word.py +99 -54
  23. knit_graphs/artin_wale_braids/Wale_Group.py +136 -88
  24. knit_graphs/{knit_graph_generators/basic_knit_graph_generators.py → basic_knit_graph_generators.py} +302 -248
  25. knit_graphs-0.0.7.dist-info/LICENSE +21 -0
  26. {knit_graphs-0.0.6.dist-info → knit_graphs-0.0.7.dist-info}/METADATA +33 -24
  27. knit_graphs-0.0.7.dist-info/RECORD +29 -0
  28. {knit_graphs-0.0.6.dist-info → knit_graphs-0.0.7.dist-info}/WHEEL +1 -1
  29. knit_graphs/__about__.py +0 -4
  30. knit_graphs/knit_graph_generators/__init__.py +0 -0
  31. knit_graphs/knit_graph_visualizer/Stitch_Visualizer.py +0 -427
  32. knit_graphs/knit_graph_visualizer/__init__.py +0 -0
  33. knit_graphs-0.0.6.dist-info/RECORD +0 -22
@@ -1,248 +1,302 @@
1
- """Module of functions that generate basic knit graph swatches."""
2
-
3
- from knit_graphs.Knit_Graph import Knit_Graph
4
- from knit_graphs.Pull_Direction import Pull_Direction
5
- from knit_graphs.Yarn import Yarn
6
- from knit_graphs.artin_wale_braids.Crossing_Direction import Crossing_Direction
7
-
8
-
9
- def co_loops(width: int) -> tuple[Knit_Graph, Yarn]:
10
- """
11
- :param width:
12
- :return: Knit Graph with one course of length width
13
- """
14
- knit_graph = Knit_Graph()
15
- yarn = Yarn()
16
- for _ in range(0, width):
17
- _loop = yarn.make_loop_on_end(knit_graph)
18
- return knit_graph, yarn
19
-
20
-
21
- def jersey_swatch(width: int, height: int) -> Knit_Graph:
22
- """
23
- :param width: number of stitches per course
24
- :param height: number of loops per course
25
- :return: Generate a Knitgraph of width and height with all knit stitches in a sheet structure
26
- """
27
- knit_graph, yarn = co_loops(width)
28
- last_course = knit_graph.get_courses()[0]
29
- for _ in range(0, height):
30
- next_course = []
31
- for parent_loop in reversed(last_course):
32
- child_loop = yarn.make_loop_on_end(knit_graph)
33
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
34
- next_course.append(child_loop)
35
- last_course = next_course
36
- return knit_graph
37
-
38
-
39
- def jersey_tube(tube_width: int, height: int) -> Knit_Graph:
40
- """
41
- :param tube_width: number of stitches per course on front side of tube
42
- :param height: number of loops per course
43
- :return: Generate a Knitgraph of width and height with all knit stitches in a tube structure
44
- """
45
- knit_graph, yarn = co_loops(tube_width * 2)
46
- last_course = [*knit_graph.get_courses()[0]]
47
-
48
- def _set_tube_floats():
49
- front_loops = last_course[0:tube_width]
50
- back_loops = last_course[tube_width:]
51
- for first_front, second_front, back in zip(front_loops[0:-1], front_loops[1:], reversed(back_loops)):
52
- yarn.add_loop_behind_float(back, first_front, second_front)
53
- for (first_back, second_back, front) in zip(back_loops[0:-1], back_loops[1:], reversed(front_loops)):
54
- yarn.add_loop_in_front_of_float(front, first_back, second_back)
55
-
56
- _set_tube_floats()
57
- for _ in range(0, height):
58
- next_course = [yarn.make_loop_on_end(knit_graph) for _p in last_course]
59
- for parent_loop, child_loop in zip(last_course, next_course):
60
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
61
- last_course = next_course
62
- _set_tube_floats()
63
- return knit_graph
64
-
65
-
66
- def kp_rib_swatch(width: int, height: int) -> Knit_Graph:
67
- """
68
- :param width: number of stitches per course
69
- :param height: number of loops per course
70
- :return: Generate a Knitgraph of width and height with alternating wales of knit purl stitches in a sheet structure
71
- """
72
- knit_graph, yarn = co_loops(width)
73
- last_course = knit_graph.get_courses()[0]
74
- next_course = []
75
- next_pull = Pull_Direction.BtF
76
- for parent_loop in reversed(last_course):
77
- child_loop = yarn.make_loop_on_end(knit_graph)
78
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
79
- next_pull = next_pull.opposite()
80
- next_course.append(child_loop)
81
- last_course = next_course
82
- for _ in range(1, height):
83
- next_course = []
84
- for parent_loop in reversed(last_course):
85
- grand_parent = parent_loop.parent_loops[0]
86
- parent_pull = knit_graph.get_stitch_edge(grand_parent, parent_loop, "pull_direction")
87
- child_loop = yarn.make_loop_on_end(knit_graph)
88
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=parent_pull)
89
- next_course.append(child_loop)
90
- last_course = next_course
91
- return knit_graph
92
-
93
-
94
- def seed_swatch(width: int, height: int) -> Knit_Graph:
95
- """
96
- :param width: number of stitches per course
97
- :param height: number of loops per course
98
- :return: Generate a Knitgraph of width and height with checkered knit purl stitches in a sheet structure
99
- """
100
- knit_graph, yarn = co_loops(width)
101
- last_course = knit_graph.get_courses()[0]
102
- next_course = []
103
- next_pull = Pull_Direction.BtF
104
- for parent_loop in reversed(last_course):
105
- child_loop = yarn.make_loop_on_end(knit_graph)
106
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
107
- next_pull = next_pull.opposite()
108
- next_course.append(child_loop)
109
- last_course = next_course
110
- for _ in range(1, height):
111
- next_course = []
112
- for parent_loop in reversed(last_course):
113
- grand_parent = parent_loop.parent_loops[0]
114
- parent_pull = knit_graph.get_stitch_edge(grand_parent, parent_loop, "pull_direction")
115
- child_loop = yarn.make_loop_on_end(knit_graph)
116
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=parent_pull.opposite())
117
- next_course.append(child_loop)
118
- last_course = next_course
119
- return knit_graph
120
-
121
-
122
- def kp_mesh_decrease_left_swatch(width, height) -> Knit_Graph:
123
- """
124
- :param width: number of stitches per course
125
- :param height: number of loops per course
126
- :return: Knit Graph with mesh of kp rib with even courses decreasing purls leftward and replacing them with yarn overs
127
- """
128
- # k<o k<o k <-: 1->2
129
- # |\ |\
130
- # k p k p k ->: 0->1
131
- # 0 1 2 3 4
132
- knit_graph, yarn = co_loops(width)
133
- last_course = knit_graph.get_courses()[0]
134
- next_course = []
135
- next_pull = Pull_Direction.BtF
136
- for parent_loop in reversed(last_course):
137
- child_loop = yarn.make_loop_on_end(knit_graph)
138
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
139
- next_pull = next_pull.opposite()
140
- next_course.append(child_loop)
141
- last_course = next_course
142
- for _ in range(1, height):
143
- next_course = []
144
- for parent_loop in reversed(last_course):
145
- child_loop = yarn.make_loop_on_end(knit_graph)
146
- grand_parent = parent_loop.parent_loops[0]
147
- parent_pull = knit_graph.get_stitch_edge(grand_parent, parent_loop, "pull_direction")
148
- if parent_pull is Pull_Direction.BtF: # knits stay in decrease at bottom of stack
149
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF, stack_position=0)
150
- prior_parent = yarn.prior_loop(parent_loop)
151
- if prior_parent is not None:
152
- knit_graph.connect_loops(prior_parent, child_loop, pull_direction=Pull_Direction.FtB, stack_position=1)
153
- next_course.append(child_loop)
154
- last_course = next_course
155
- next_course = []
156
- for parent_loop in reversed(last_course):
157
- child_loop = yarn.make_loop_on_end(knit_graph)
158
- if len(parent_loop.parent_loops) == 0:
159
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.FtB)
160
- else:
161
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
162
- next_course.append(child_loop)
163
- last_course = next_course
164
- return knit_graph
165
-
166
-
167
- def kp_mesh_decrease_right_swatch(width, height) -> Knit_Graph:
168
- """
169
- :param width: number of stitches per course
170
- :param height: number of loops per course
171
- :return: Knit Graph with mesh of kp rib with even courses decreasing purls rightward and replacing them with yarn overs
172
- """
173
- # k o>k o>k <-: 1->2
174
- # /| /|
175
- # k p k p k ->: 0->1
176
- # 0 1 2 3 4
177
- knit_graph, yarn = co_loops(width)
178
- last_course = knit_graph.get_courses()[0]
179
- next_course = []
180
- next_pull = Pull_Direction.BtF
181
- for parent_loop in reversed(last_course):
182
- child_loop = yarn.make_loop_on_end(knit_graph)
183
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
184
- next_pull = next_pull.opposite()
185
- next_course.append(child_loop)
186
- last_course = next_course
187
- for _ in range(1, height):
188
- next_course = []
189
- for parent_loop in reversed(last_course):
190
- child_loop = yarn.make_loop_on_end(knit_graph)
191
- grand_parent = parent_loop.parent_loops[0]
192
- parent_pull = knit_graph.get_stitch_edge(grand_parent, parent_loop, "pull_direction")
193
- if parent_pull is Pull_Direction.BtF: # knits stay in decrease at bottom of stack
194
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF, stack_position=0)
195
- next_parent = yarn.next_loop(parent_loop)
196
- if next_parent is not None:
197
- knit_graph.connect_loops(next_parent, child_loop, pull_direction=Pull_Direction.FtB, stack_position=1)
198
- next_course.append(child_loop)
199
- last_course = next_course
200
- next_course = []
201
- for parent_loop in reversed(last_course):
202
- child_loop = yarn.make_loop_on_end(knit_graph)
203
- if len(parent_loop.parent_loops) == 0:
204
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.FtB)
205
- else:
206
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
207
- next_course.append(child_loop)
208
- last_course = next_course
209
- return knit_graph
210
-
211
-
212
- def twist_cable(width, height) -> Knit_Graph:
213
- """
214
- :param width: number of stitches per course
215
- :param height: number of loops per course
216
- :return: Knit Graph of alternating 1 by 1 twists in different directions with purl wales between them
217
- """
218
- # p k\k p ->: 3-4
219
- # p k k p <-: 2-3
220
- # p k/k p ->: 1-2
221
- # p k k p <-: 0-1
222
- # 0 1 2 3
223
- knit_graph, yarn = co_loops(width)
224
- last_course = knit_graph.get_courses()[0]
225
- next_course = []
226
- pull_directions = [Pull_Direction.FtB, Pull_Direction.BtF, Pull_Direction.BtF, Pull_Direction.FtB]
227
- for i, parent_loop in enumerate(reversed(last_course)):
228
- child_loop = yarn.make_loop_on_end(knit_graph)
229
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=pull_directions[i % 4])
230
- next_course.append(child_loop)
231
- last_course = next_course
232
- crossing = Crossing_Direction.Over_Right
233
- for r in range(1, height):
234
- next_course = [yarn.make_loop_on_end(knit_graph) for _ in last_course]
235
- for i, parent_loop in enumerate(reversed(last_course)):
236
- if r % 2 == 0 or i % 4 == 0 or i % 4 == 3: # not cable row (even) or in purl wale
237
- child_loop = next_course[i]
238
- elif i % 4 == 1:
239
- child_loop = next_course[i + 1]
240
- else:
241
- child_loop = next_course[i - 1]
242
- knit_graph.connect_loops(parent_loop, child_loop, pull_direction=pull_directions[i % 4])
243
- if r % 2 == 1: # cable row
244
- for left_loop, right_loop in zip(next_course[1::4], next_course[2::4]):
245
- knit_graph.add_crossing(left_loop, right_loop, crossing)
246
- crossing = ~crossing
247
- last_course = next_course
248
- return knit_graph
1
+ """Module of functions that generate basic knit graph swatches.
2
+
3
+ This module provides utility functions for creating common knitting patterns and structures as knit graphs.
4
+ These functions serve as building blocks for testing and demonstration purposes.
5
+ """
6
+ from knit_graphs.artin_wale_braids.Crossing_Direction import Crossing_Direction
7
+ from knit_graphs.Knit_Graph import Knit_Graph
8
+ from knit_graphs.Pull_Direction import Pull_Direction
9
+ from knit_graphs.Yarn import Yarn
10
+
11
+
12
+ def co_loops(width: int) -> tuple[Knit_Graph, Yarn]:
13
+ """Create a cast-on row of loops forming the foundation for knitting patterns.
14
+
15
+ Args:
16
+ width (int): The number of loops to create in the cast-on row.
17
+
18
+ Returns:
19
+ tuple[Knit_Graph, Yarn]: A tuple containing the knit graph with one course of the specified width and the yarn used to create it.
20
+ """
21
+ knit_graph = Knit_Graph()
22
+ yarn = Yarn(knit_graph=knit_graph)
23
+ for _ in range(0, width):
24
+ _loop = yarn.make_loop_on_end()
25
+ return knit_graph, yarn
26
+
27
+
28
+ def jersey_swatch(width: int, height: int) -> Knit_Graph:
29
+ """Generate a rectangular knit swatch with all knit stitches in a flat sheet structure.
30
+
31
+ This creates a basic stockinette/jersey pattern where all stitches are worked as knit stitches from back to front.
32
+
33
+ Args:
34
+ width (int): The number of stitches per course (horizontal row).
35
+ height (int): The number of courses (vertical rows) in the swatch.
36
+
37
+ Returns:
38
+ Knit_Graph: A knit graph representing a flat rectangular swatch with all knit stitches.
39
+ """
40
+ knit_graph, yarn = co_loops(width)
41
+ last_course = list(knit_graph.get_courses()[0])
42
+ for _ in range(0, height):
43
+ next_course = []
44
+ for parent_loop in reversed(last_course):
45
+ child_loop = yarn.make_loop_on_end()
46
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
47
+ next_course.append(child_loop)
48
+ last_course = next_course
49
+ return knit_graph
50
+
51
+
52
+ def jersey_tube(tube_width: int, height: int) -> Knit_Graph:
53
+ """Generate a tubular knit structure with all knit stitches worked in the round.
54
+
55
+ This creates a seamless tube by knitting in the round, where the front and back sections are connected by floats to maintain the circular structure.
56
+
57
+ Args:
58
+ tube_width (int): The number of stitches per course on the front side of the tube.
59
+ height (int): The number of courses (vertical rows) in the tube.
60
+
61
+ Returns:
62
+ Knit_Graph: A knit graph representing a seamless tube with all knit stitches.
63
+ """
64
+ knit_graph, yarn = co_loops(tube_width * 2)
65
+ last_course = [*knit_graph.get_courses()[0]]
66
+
67
+ def _set_tube_floats() -> None:
68
+ """Internal helper function to set up float connections between front and back of tube."""
69
+ front_loops = last_course[0:tube_width]
70
+ back_loops = last_course[tube_width:]
71
+ for first_front, second_front, back in zip(front_loops[0:-1], front_loops[1:], reversed(back_loops)):
72
+ yarn.add_loop_behind_float(back, first_front, second_front)
73
+ for (first_back, second_back, front) in zip(back_loops[0:-1], back_loops[1:], reversed(front_loops)):
74
+ yarn.add_loop_in_front_of_float(front, first_back, second_back)
75
+
76
+ _set_tube_floats()
77
+ for _ in range(0, height):
78
+ next_course = [yarn.make_loop_on_end() for _p in last_course]
79
+ for parent_loop, child_loop in zip(last_course, next_course):
80
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
81
+ last_course = next_course
82
+ _set_tube_floats()
83
+ return knit_graph
84
+
85
+
86
+ def kp_rib_swatch(width: int, height: int) -> Knit_Graph:
87
+ """Generate a knit-purl ribbing swatch with alternating wales of knit and purl stitches.
88
+
89
+ This creates a 1x1 ribbing pattern where knit and purl wales alternate, maintaining their stitch type throughout the height of the swatch for a stretchy, textured fabric.
90
+
91
+ Args:
92
+ width (int): The number of stitches per course (horizontal row).
93
+ height (int): The number of courses (vertical rows) in the swatch.
94
+
95
+ Returns:
96
+ Knit_Graph: A knit graph representing a ribbed swatch with alternating knit and purl wales.
97
+ """
98
+ knit_graph, yarn = co_loops(width)
99
+ last_course = knit_graph.get_courses()[0]
100
+ next_course = []
101
+ next_pull = Pull_Direction.BtF
102
+ for parent_loop in reversed(last_course):
103
+ child_loop = yarn.make_loop_on_end()
104
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
105
+ next_pull = next_pull.opposite()
106
+ next_course.append(child_loop)
107
+ last_course = next_course
108
+ for _ in range(1, height):
109
+ next_course = []
110
+ for parent_loop in reversed(last_course):
111
+ grand_parent = parent_loop.parent_loops[0]
112
+ parent_pull = knit_graph.get_pull_direction(grand_parent, parent_loop)
113
+ assert isinstance(parent_pull, Pull_Direction)
114
+ child_loop = yarn.make_loop_on_end()
115
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=parent_pull)
116
+ next_course.append(child_loop)
117
+ last_course = next_course
118
+ return knit_graph
119
+
120
+
121
+ def seed_swatch(width: int, height: int) -> Knit_Graph:
122
+ """Generate a seed stitch swatch with a checkerboard pattern of knit and purl stitches.
123
+
124
+ This creates a textured fabric where each stitch alternates between knit and purl both horizontally and vertically, creating a bumpy, non-curling fabric texture.
125
+
126
+ Args:
127
+ width (int): The number of stitches per course (horizontal row).
128
+ height (int): The number of courses (vertical rows) in the swatch.
129
+
130
+ Returns:
131
+ Knit_Graph: A knit graph representing a seed stitch swatch with checkerboard knit-purl pattern.
132
+ """
133
+ knit_graph, yarn = co_loops(width)
134
+ last_course = knit_graph.get_courses()[0]
135
+ next_course = []
136
+ next_pull = Pull_Direction.BtF
137
+ for parent_loop in reversed(last_course):
138
+ child_loop = yarn.make_loop_on_end()
139
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
140
+ next_pull = next_pull.opposite()
141
+ next_course.append(child_loop)
142
+ last_course = next_course
143
+ for _ in range(1, height):
144
+ next_course = []
145
+ for parent_loop in reversed(last_course):
146
+ grand_parent = parent_loop.parent_loops[0]
147
+ parent_pull = knit_graph.get_pull_direction(grand_parent, parent_loop)
148
+ assert isinstance(parent_pull, Pull_Direction)
149
+ child_loop = yarn.make_loop_on_end()
150
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=parent_pull.opposite())
151
+ next_course.append(child_loop)
152
+ last_course = next_course
153
+ return knit_graph
154
+
155
+
156
+ def kp_mesh_decrease_left_swatch(width: int, height: int) -> Knit_Graph:
157
+ """Generate a mesh pattern with left-leaning decreases and yarn overs in knit-purl ribbing.
158
+
159
+ This creates an openwork mesh pattern where purl stitches are decreased leftward on even courses and replaced with yarn overs,
160
+ creating decorative holes in the fabric while maintaining the overall stitch count.
161
+
162
+ Args:
163
+ width (int): The number of stitches per course (horizontal row).
164
+ height (int): The number of courses (vertical rows) in the swatch.
165
+
166
+ Returns:
167
+ Knit_Graph: A knit graph representing a mesh swatch with left-leaning decreases and yarn overs.
168
+ """
169
+ # k<o k<o k <-: 1->2
170
+ # |\ |\
171
+ # k p k p k ->: 0->1
172
+ # 0 1 2 3 4
173
+ knit_graph, yarn = co_loops(width)
174
+ last_course = knit_graph.get_courses()[0]
175
+ next_course = []
176
+ next_pull = Pull_Direction.BtF
177
+ for parent_loop in reversed(last_course):
178
+ child_loop = yarn.make_loop_on_end()
179
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
180
+ next_pull = next_pull.opposite()
181
+ next_course.append(child_loop)
182
+ last_course = next_course
183
+ for _ in range(1, height):
184
+ next_course = []
185
+ for parent_loop in reversed(last_course):
186
+ child_loop = yarn.make_loop_on_end()
187
+ grand_parent = parent_loop.parent_loops[0]
188
+ parent_pull = knit_graph.get_pull_direction(grand_parent, parent_loop)
189
+ if parent_pull is Pull_Direction.BtF: # knits stay in decrease at bottom of stack
190
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF, stack_position=0)
191
+ prior_parent = yarn.prior_loop(parent_loop)
192
+ if prior_parent is not None and prior_parent in last_course:
193
+ knit_graph.connect_loops(prior_parent, child_loop, pull_direction=Pull_Direction.FtB, stack_position=1)
194
+ next_course.append(child_loop)
195
+ last_course = next_course
196
+ next_course = []
197
+ for parent_loop in reversed(last_course):
198
+ child_loop = yarn.make_loop_on_end()
199
+ if len(parent_loop.parent_loops) == 0:
200
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.FtB)
201
+ else:
202
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
203
+ next_course.append(child_loop)
204
+ last_course = next_course
205
+ return knit_graph
206
+
207
+
208
+ def kp_mesh_decrease_right_swatch(width: int, height: int) -> Knit_Graph:
209
+ """Generate a mesh pattern with right-leaning decreases and yarn overs in knit-purl ribbing.
210
+
211
+ This creates an openwork mesh pattern where purl stitches are decreased rightward on even courses and replaced with yarn overs,
212
+ creating decorative holes in the fabric while maintaining the overall stitch count.
213
+
214
+ Args:
215
+ width (int): The number of stitches per course (horizontal row).
216
+ height (int): The number of courses (vertical rows) in the swatch.
217
+
218
+ Returns:
219
+ Knit_Graph: A knit graph representing a mesh swatch with right-leaning decreases and yarn overs.
220
+ """
221
+ # k o>k o>k <-: 1->2
222
+ # /| /|
223
+ # k p k p k ->: 0->1
224
+ # 0 1 2 3 4
225
+ knit_graph, yarn = co_loops(width)
226
+ last_course = knit_graph.get_courses()[0]
227
+ next_course = []
228
+ next_pull = Pull_Direction.BtF
229
+ for parent_loop in reversed(last_course):
230
+ child_loop = yarn.make_loop_on_end()
231
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=next_pull)
232
+ next_pull = next_pull.opposite()
233
+ next_course.append(child_loop)
234
+ last_course = next_course
235
+ for _ in range(1, height):
236
+ next_course = []
237
+ for parent_loop in reversed(last_course):
238
+ child_loop = yarn.make_loop_on_end()
239
+ grand_parent = parent_loop.parent_loops[0]
240
+ parent_pull = knit_graph.get_pull_direction(grand_parent, parent_loop)
241
+ if parent_pull is Pull_Direction.BtF: # knits stay in decrease at bottom of stack
242
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF, stack_position=0)
243
+ next_parent = yarn.next_loop(parent_loop)
244
+ if next_parent is not None:
245
+ knit_graph.connect_loops(next_parent, child_loop, pull_direction=Pull_Direction.FtB, stack_position=1)
246
+ next_course.append(child_loop)
247
+ last_course = next_course
248
+ next_course = []
249
+ for parent_loop in reversed(last_course):
250
+ child_loop = yarn.make_loop_on_end()
251
+ if len(parent_loop.parent_loops) == 0:
252
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.FtB)
253
+ else:
254
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=Pull_Direction.BtF)
255
+ next_course.append(child_loop)
256
+ last_course = next_course
257
+ return knit_graph
258
+
259
+
260
+ def twist_cable(width: int, height: int) -> Knit_Graph:
261
+ """Generate a twisted cable pattern with alternating crossing directions and purl separators.
262
+
263
+ This creates a cable pattern with 1x1 twists that alternate direction every two rows, separated by purl wales to make the cable structure more prominent.
264
+
265
+ Args:
266
+ width (int): The number of stitches per course (horizontal row).
267
+ height (int): The number of courses (vertical rows) in the swatch.
268
+
269
+ Returns:
270
+ Knit_Graph: A knit graph representing a twisted cable pattern with alternating crossing directions.
271
+ """
272
+ # p k\k p ->: 3-4
273
+ # p k k p <-: 2-3
274
+ # p k/k p ->: 1-2
275
+ # p k k p <-: 0-1
276
+ # 0 1 2 3
277
+ knit_graph, yarn = co_loops(width)
278
+ last_course = knit_graph.get_courses()[0]
279
+ next_course = []
280
+ pull_directions = [Pull_Direction.FtB, Pull_Direction.BtF, Pull_Direction.BtF, Pull_Direction.FtB]
281
+ for i, parent_loop in enumerate(reversed(last_course)):
282
+ child_loop = yarn.make_loop_on_end()
283
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=pull_directions[i % 4])
284
+ next_course.append(child_loop)
285
+ last_course = next_course
286
+ crossing = Crossing_Direction.Over_Right
287
+ for r in range(1, height):
288
+ next_course = [yarn.make_loop_on_end() for _ in last_course]
289
+ for i, parent_loop in enumerate(reversed(last_course)):
290
+ if r % 2 == 0 or i % 4 == 0 or i % 4 == 3: # not cable row (even) or in purl wale
291
+ child_loop = next_course[i]
292
+ elif i % 4 == 1:
293
+ child_loop = next_course[i + 1]
294
+ else:
295
+ child_loop = next_course[i - 1]
296
+ knit_graph.connect_loops(parent_loop, child_loop, pull_direction=pull_directions[i % 4])
297
+ if r % 2 == 1: # cable row
298
+ for left_loop, right_loop in zip(next_course[1::4], next_course[2::4]):
299
+ knit_graph.add_crossing(left_loop, right_loop, crossing)
300
+ crossing = ~crossing
301
+ last_course = next_course
302
+ return knit_graph
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Megan Hofmann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.