set insertdvicanvas true after insertion
[PyX/mjg.git] / examples / vector.py
blob86a2764531e66b72e11012824c18eec28d3c65aa
1 from math import pi, sin, cos
2 from pyx import *
4 def vector(x1, y1, x2, y2, t, pos=0.5, distance=0.1,
5 texrunner=text.defaulttexrunner):
6 c = canvas.canvas()
7 c.stroke(path.line(x1, y1, x2, y2), [deco.earrow.normal])
8 textbox = texrunner.text((1-pos)*x1 + pos*x2, (1-pos)*y1 + pos*y2, t,
9 [text.halign.center, text.vshift.mathaxis])
10 if distance < 0:
11 textbox.linealign(-distance, y1 - y2, x2 - x1)
12 else:
13 textbox.linealign(distance, y2 - y1, x1 - x2)
14 c.insert(textbox)
15 return c
17 r = 1.5
18 a = 150
20 c = canvas.canvas()
21 dx, dy = cos(a * pi / 180), sin(a * pi / 180)
22 x, y = r * dx, r * dy
23 c.stroke(path.circle(0, 0, r))
24 c.insert(vector(0, 0, x, y, r"$\vec r$"))
25 c.insert(vector(x, y, x - dy, y + dx, r"$\vec t$", pos=0.7))
26 c.insert(vector(x, y, x + dx, y + dy, r"$\vec n$", pos=0.7))
27 c.writetofile("vector")