Draw intertwined threads, knots and knitting figures with PyCairo
Overall Idea
Threads (paths) are made of segments (Bezier curves) with their own z-depth.
Segments are drawn in z-depth order, so that threads look Intertwined.
Control points are either set explicitely by the caller, or computed according to specific rules.
Here are the drawing steps, starting from lowest z-depth, here for Braid.py
.
Architecture
There are three classes, stacked in a layer model.
Canvas - the surface where to draw
Thread - a specific thread, attached to a canvas
Segment - a portion of a thread, with its own z-depth
API
The caller only interacts with Canvas, which exposes methods to create and move threads.
cv = Canvas(...)
t = cv.create_thread(...)
cv.arc_to(t, ...)
cv.arc_rel(t, ...)
cv.draw(...)
Whenever a thread is moved, a segment is added to the thread.
Segments are cubic Bezier curves, defined by two endpoints and two control points.
Control points logic
When not set by the caller, control points are calculated as follows:
cp1
: symetric to previous segement cp2 around segment origincp2
: from segment end towards segment cp1, same length as origin to cp1
Sample Drawings
Crochet.py
from Intertwined import *
cv = Canvas(47, 25)
v_offset = 6
for row in range(3):
t = cv.create_thread(o = (1, 10 + (v_offset*row)))
for col in range(3):
cv.arc_rel(t, 8, 0, 1, cp1=(2, 2), cp2=(-2, 2))
cv.arc_rel(t, -2, -8, 1, cp2=(-2, 2))
cv.arc_rel(t, 8, 0, 0, cp2=(-2,-2))
cv.arc_rel(t, -2, 8, 1, cp2=(-2,-2))
cv.arc_rel(t, 8, 0, 1, cp2=(-2, 2))
cv.draw()
cv.surface.write_to_png("crochet.png")
Knots.py
Node.py
Drawing Options