# PyX — Gallery: path/knot.py

2.6 KB
37.5 KB
9.3 KB
2.7 KB
8.3 KB

## Paths with constant distance ```from pyx import *
unit.set(uscale=3, wscale=3)

dist = 0.15           # distance between the ropes
thick = 0.08          # thickness of the ropes

# build the basepath of the knot. This is the curve that lies
# between both ropes.
A = 1.0, 0.1    # point where the knot starts after the straight line
B = 1.3, 0      # point where the knot is curved, overlying the straight line
t = -0.8, -0.1  # direction at A
s =  0.3, 1.2   # direction at B
pts = [
( A-t, A-t), ( A     , A     ),
( A+t, A+t), (-B-s,-B-s),
(-B     ,-B     ), (-B+s,-B+s),
( B-s, B-s), ( B     , B     ),
( B+s, B+s), (-A-t,-A-t),
(-A     ,-A     ), (-A+t,-A+t) ]
seam = path.path(
path.moveto(*(pts)),
path.lineto(*(pts)),
path.curveto(pts,pts,pts,pts,pts,pts),
path.curveto(pts,pts,pts,pts,pts,pts),
path.curveto(pts,pts,pts,pts,pts,pts),
path.lineto(*(pts)) )
# We smooth this curve a little because we used curveto together with lineto
seam = deformer.smoothed(0.6).deform(seam)

# The ropes, when drawn later, will have to overlap in a very specific way.
l = seam.arclen()
epsilon = 0.002*l # small overlap between the segments
seam_segs = seam.split([0.28*l+epsilon, 0.28*l-epsilon,
0.42*l+epsilon, 0.42*l-epsilon,
0.58*l+epsilon, 0.58*l-epsilon,
0.72*l+epsilon, 0.72*l-epsilon])[::2]

# For each segment, two shifted paths build the boundaries of each rope
ropes_segs = []
for seam_seg in seam_segs:
ropes_segs.append([])
for ropeshift in [-0.5*dist, 0.5*dist]:
ropes_segs[-1].append([])
for edgeshift in [-0.5*thick, 0.5*thick]:
ropes_segs[-1][-1].append(
deformer.parallel(ropeshift + edgeshift).deform(seam_seg))
# Now, ropes_segs is a list of segments, containing a list of ropes,
# each containing the two bounding paths of the rope segment

rope_colors = [color.rgb.blue, color.rgb.red]
c = canvas.canvas()
# Because the segments should overlap in a very specific manner
# we have to draw them in the correct order
for index in [1, 4, 2, 0, 3]:
for rope_seg, col in zip(ropes_segs[index], rope_colors):
seg = rope_seg.joined(rope_seg.reversed())
seg.append(path.closepath())
c.fill(seg, [col])
c.stroke(rope_seg, [style.linecap.round, style.linejoin.round])
c.stroke(rope_seg, [style.linecap.round, style.linejoin.round])

c.writeEPSfile()
c.writePDFfile()
c.writeSVGfile()
```

### Description

The example shows how to create paths with constant distance. This is achieved with the aid of `deformer.parallel`, which has already been used in the above example.

The first part of the example code is only for creating the middle line of the knot. In order to obtain a nicely curved rope, we smooth the such defined curve a bit.

The individual parts of the knot must be stroked in a very specific way in order to achieve the correct overlapping of the knot. To this end, we split the base curve into five pieces.

The key element of the example is the use of `deformer.parallel` which creates the curves with constant distance from the base curve. We apply the deformer four times, thus creating the outline path of two individual ropes, which parallel follow the shape of the knot.