centered graph key alignment
[PyX/mjg.git] / design / boxalign.py
blob77efea5d90054df70387c307cdb1ee6634c2f2db
1 #!/usr/bin/env python
2 import sys, math
3 sys.path[:0] = [".."]
4 from pyx import *
6 c = canvas.canvas()
7 c.stroke(path.line(0, 0, 4, 2), deco.earrow.Large())
8 t = text.text(2, 1, r"$\vec p$")
9 t.linealign(0.1, -2/math.sqrt(20), 4/math.sqrt(20))
10 c.insert(t)
11 c.stroke(path.line(0, 0, 1, 1), deco.earrow.Large())
12 t = text.text(0.5, 0.5, r"$\vec a$")
13 t.linealign(0.1, -1/math.sqrt(2), 1/math.sqrt(2))
14 c.insert(t)
15 c.stroke(path.line(-1, 3, 3, -1), style.linestyle.dotted)
16 c.stroke(path.line(1, 1, 1-1/math.sqrt(2), 1+1/math.sqrt(2)), deco.earrow.Large())
17 t = text.text(1-1/math.sqrt(8), 1+1/math.sqrt(8), r"$\vec b$")
18 t.linealign(0.1, 1/math.sqrt(2), 1/math.sqrt(2))
19 c.insert(t)
20 c.writetofile("boxalignpal")
22 ###############################################################################
24 c = canvas.canvas()
25 c.stroke(path.line(0, 0, 4, 2), deco.earrow.Large())
26 t = text.text(2, 1, r"$\vec p$")
27 t.linealign(0.1, -2/math.sqrt(20), 4/math.sqrt(20))
28 c.insert(t)
29 c.stroke(path.line(0, 0, -1, 1), deco.earrow.Large())
30 t = text.text(-0.5, 0.5, r"$\vec a$")
31 t.linealign(0.1, -1/math.sqrt(2), -1/math.sqrt(2))
32 c.insert(t)
33 c.stroke(path.path(path.arc(0, 0, math.sqrt(2), 0, 360), path.closepath()), style.linestyle.dotted)
34 c.writetofile("boxalignpac")
36 ###############################################################################
38 c = canvas.canvas()
39 c.stroke(path.line(0, 0, 4, 2), deco.earrow.Large())
40 t = text.text(2, 1, r"$\vec e$")
41 t.linealign(0.1, -2/math.sqrt(20), 4/math.sqrt(20))
42 c.insert(t)
43 c.stroke(path.line(0, 0, 6, 2), deco.earrow.Large())
44 t = text.text(3, 1, r"$\vec f$")
45 t.linealign(0.1, 2/math.sqrt(60), -6/math.sqrt(60))
46 c.insert(t)
47 c.stroke(path.line(6, 2, 4, 2), deco.earrow.Large())
48 t = text.text(5, 2, r"$\vec g$")
49 t.linealign(0.1, 0, 1)
50 c.insert(t)
51 c.stroke(path.line(0, 0, -1, 1), deco.earrow.Large())
52 t = text.text(-0.5, 0.5, r"$\vec a$")
53 t.linealign(0.1, -1/math.sqrt(2), -1/math.sqrt(2))
54 c.insert(t)
55 c.stroke(path.path(path.arc(0, 0, math.sqrt(2), 0, 360), path.closepath()), style.linestyle.dotted)
56 c.stroke(path.line(0, 0, 0, math.sqrt(2)), deco.earrow.Large())
57 t = text.text(0, 1/math.sqrt(2), r"$\vec b$")
58 t.linealign(0.1, -1, 0)
59 c.insert(t)
60 c.writetofile("boxalignlac")
62 ###############################################################################
64 def drawexample(canvas, corner, linealign):
65 r = 1.5
66 canvas.stroke(path.path(path.arc(0, 0, r, 0, 360)))
67 phi = 0
68 while phi < 2 * math.pi + 1e-10:
69 if corner:
70 b = box.polygon(center=(0, 0), corners=[(0, 0), (1, 0), (0.5, math.sqrt(3)/2)])
71 else:
72 b = box.polygon(center=(0, 0), corners=[(-0.5, -math.sqrt(3)/6), (0.5, -math.sqrt(3)/6), (0, math.sqrt(3)/3)])
73 if linealign:
74 b.linealign(r, math.cos(phi), math.sin(phi))
75 else:
76 b.circlealign(r, math.cos(phi), math.sin(phi))
77 if round(phi / math.pi * 2 * 100) % 100:
78 canvas.stroke(b.path())
79 else:
80 canvas.stroke(b.path(), color.rgb.red)
81 phi += math.pi / 50
83 d = 6
84 c = canvas.canvas()
85 sc = c.insert(canvas.canvas(trafo.translate(0, d)))
86 drawexample(sc, 0, 0)
87 sc = c.insert(canvas.canvas(trafo.translate(d, d)))
88 drawexample(sc, 0, 1)
89 sc = c.insert(canvas.canvas(trafo.translate(0, 0)))
90 drawexample(sc, 1, 0)
91 sc = c.insert(canvas.canvas(trafo.translate(d, 0)))
92 drawexample(sc, 1, 1)
93 c.text(0, 1.5*d, "align at a circle", text.halign.center)
94 c.text(d, 1.5*d, "align at tangents", text.halign.center)
95 c.text(-0.5*d, d, "reference point at the triangle center", text.halign.center, trafo.rotate(90))
96 c.text(-0.5*d, 0, "reference point at a triangle corner", text.halign.center, trafo.rotate(90))
97 c.writetofile("boxalignexample")