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