Skip to content

neozhaoliang/Intertwined

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Intertwined

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.

braid.pngbraid.gif

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 origin
  • cp2: from segment end towards segment cp1, same length as origin to cp1
No control points are set explicitely on 1st segment s1.
Control point s1.cp2 is computed as half s.p_s.o.
Control point s2.cp1 (red) is computed according to previous segment s1.cp2 (green).
Control point s2.cp2 is computed as s2.p_s2.cp1 with samed length as s2.cp1.
t = cv.create_thread(o = (1,2))
cv.arc_rel(t, 5, -1, 1)
cv.arc_rel(t, 2, 5, 1)
s1.cp1 is set, s1.cp2 is calculated accordingly.
Orientation propagates to s2.cp1.
t = cv.create_thread(o = (1,2))
cv.arc_rel(t, 5, -1, 1, cp1=(1,-2))
cv.arc_rel(t, 2, 5, 1)
s1.cp2 is set, s2.cp1 is calculated accordingly.
t = cv.create_thread(o = (1,2))
cv.arc_rel(t, 5, -1, 1, cp2=(0,-2))
cv.arc_rel(t, 2, 5, 1)
Both control points are set for s1.
t = cv.create_thread(o = (1,2))
cv.arc_rel(t, 5, -1, 1, cp1=(1,-2), cp2=(0,-2))
cv.arc_rel(t, 2, 5, 1)

Sample Drawings

Crochet.py

crochet.png

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

knots.png


Node.py

knots.png


Drawing Options

Draw Grid
cv.draw(draw_grid=True)
Stretch Grid
cv = Canvas(10, 8, col_size=20, row_size=30)
Draw Points
cv.draw(draw_points=True)
Draw Control Points
cv.draw(draw_cp=True)
Draw Depth
cv.draw(draw_depth=True)
Draw All
cv.draw(draw_grid=True,
    draw_points=True,
    draw_cp=True,
    draw_depth=True)

About

Draw intertwined threads, knots and knitting figures with PyCairo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%