remove the gallery, it is now a wiki at https://sourceforge.net/p/pyx/gallery
[PyX.git] / manual / connector.py
blob9112edbb02d287ad7e46f6510b5fad191da70fdf
1 from math import *
2 from pyx import *
3 try:
4 from math import radians, degrees
5 except ImportError:
6 # fallback implementation for Python 2.1
7 def radians(x): return x*pi/180
8 def degrees(x): return x*180/pi
10 startbox = box.polygon(corners=[[0,-0.6], [0.5,0.1], [-0.25,0.1]])
11 endbox = box.polygon(corners=[[4.5,3.9], [5.5,4.0], [5.2,3.4]])
13 # the arc connector <<<
14 c1 = canvas.canvas()
15 for b in [startbox, endbox]:
16 c1.stroke(b.path(), [style.linewidth.Thick, style.linejoin.round])
17 c1.fill(path.circle_pt(b.center[0], b.center[1], 2))
18 absangle = degrees(atan2(endbox.center[1] - startbox.center[1], endbox.center[0] - startbox.center[0]))
19 relangle = 60
20 len = 2
22 # the direct connection
23 direct = path.line_pt(startbox.center[0], startbox.center[1], endbox.center[0], endbox.center[1])
24 c1.stroke(direct, [style.linestyle.dashed])
26 # the arc connector
27 l = connector.arc(startbox, endbox, relangle=relangle, boxdists=[0.0,0.0])
28 c1.stroke(l, [style.linewidth.Thick, color.rgb.red, deco.earrow.Large])
30 # the relangle parameter
31 comp1 = path.path(path.moveto(*direct.atbegin()),
32 path.rlineto(len*cos(radians(absangle + relangle)), len*sin(radians(absangle + relangle))))
33 c1.stroke(comp1)
34 ang = path.path(path.arc(direct.atbegin()[0], direct.atbegin()[1], 0.8*len, absangle, absangle+relangle))
35 c1.stroke(ang, [deco.earrow.large])
36 pos = ang.at(0.5*ang.arclen())
37 c1.text(pos[0], pos[1], r"~relangle", [text.halign.left])
39 # the bulge parameter
40 bulge = 0.5 * direct.arclen() * tan(0.5*radians(relangle))
41 bul = path.path(path.moveto(*direct.at(0.5*direct.arclen())),
42 path.rlineto(bulge * cos(radians(absangle+90)), bulge * sin(radians(absangle+90))))
43 c1.stroke(bul, [deco.earrow.large])
44 pos = bul.at(0.5*bul.arclen())
45 c1.text(pos[0], pos[1], r"~(rel)bulge", [text.halign.left])
47 # >>>
49 # the curve connector <<<
50 c2 = canvas.canvas()
51 for b in [startbox, endbox]:
52 c2.stroke(b.path(), [style.linewidth.Thick, style.linejoin.round])
53 c2.fill(path.circle_pt(b.center[0], b.center[1], 2))
54 absangle = degrees(atan2(endbox.center[1] - startbox.center[1], endbox.center[0] - startbox.center[0]))
55 relangle1 = 60
56 relangle2 = 30
57 absbulge = 0
58 relbulge = 0.5
59 len = 2
61 # the direct connection
62 direct = path.line_pt(startbox.center[0], startbox.center[1], endbox.center[0], endbox.center[1])
63 c2.stroke(direct, [style.linestyle.dashed])
65 # the arc connector
66 l = connector.curve(startbox, endbox, relangle1=relangle1, relangle2=relangle2, absbulge=absbulge, relbulge=relbulge, boxdists=[0.0,0.0])
67 #l = connector.curve(startbox, endbox, absangle1=absangle+relangle1, absangle2=absangle+relangle2, absbulge=absbulge, relbulge=relbulge, boxdists=[0.0,0.0])
68 c2.stroke(l, [style.linewidth.Thick, color.rgb.red, deco.earrow.Large])
70 # the relangle parameters
71 # relangle1
72 c2.stroke(path.path(path.moveto(*direct.atbegin()),
73 path.rlineto(len*cos(radians(absangle + relangle1)),
74 len*sin(radians(absangle + relangle1)))))
75 ang = path.path(path.arc(direct.atbegin()[0], direct.atbegin()[1], 0.8*len, absangle, absangle+relangle1))
76 c2.stroke(ang, [deco.earrow.large])
77 pos = ang.at(0.5*ang.arclen())
78 c2.text(pos[0], pos[1], r"~relangle1", [text.halign.left])
80 # absangle1
81 c2.stroke(path.path(path.moveto(*direct.atbegin()), path.rlineto(len, 0)))
82 ang = path.path(path.arc(direct.atbegin()[0], direct.atbegin()[1], 0.5*len, 0, absangle+relangle1))
83 c2.stroke(ang, [deco.earrow.large])
84 pos = ang.at(0.2*ang.arclen())
85 c2.text(pos[0], pos[1], r"~absangle1", [text.halign.left])
87 # relangle2
88 c2.stroke(path.path(path.moveto(*direct.atend()),
89 path.rlineto(len*cos(radians(absangle)),
90 len*sin(radians(absangle)))))
91 c2.stroke(path.path(path.moveto(*direct.atend()),
92 path.rlineto(len*cos(radians(absangle + relangle2)),
93 len*sin(radians(absangle + relangle2)))))
94 ang = path.path(path.arc(direct.atend()[0], direct.atend()[1],
95 0.8*len, absangle, absangle+relangle2))
96 c2.stroke(ang, [deco.earrow.large])
97 pos = ang.at(0.5*ang.arclen())
98 c2.text(pos[0], pos[1], r"~relangle2", [text.halign.left])
100 # the bulge parameter
101 bulge = absbulge + direct.arclen() * relbulge
102 bul = path.path(path.moveto(*direct.atbegin()),
103 path.rlineto(bulge * cos(radians(absangle+relangle1)), bulge * sin(radians(absangle+relangle1))))
104 c2.stroke(bul, [deco.earrow.large])
105 pos = bul.at(0.7*bul.arclen())
106 c2.text(pos[0], pos[1], r"~(rel)bulge", [text.halign.left])
108 bul = path.path(path.moveto(*direct.atend()),
109 path.rlineto(-bulge * cos(radians(absangle+relangle2)), -bulge * sin(radians(absangle+relangle2))))
110 c2.stroke(bul, [deco.earrow.large])
111 pos = bul.at(0.7*bul.arclen())
112 c2.text(pos[0], pos[1], r"~(rel)bulge", [text.halign.left, text.vshift(1)])
113 # >>>
116 # write everything
117 c1.insert(c2, [trafo.translate(6.5, 0)])
118 c1.writePDFfile()
120 # vim:foldmethod=marker:foldmarker=<<<,>>>