- path module
[PyX/mjg.git] / test / functional / test_path.py
blob120540e41c2803a52d96e2e6e6715d59f5932e86
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
4 from pyx import *
5 from pyx.path import *
6 from pyx import normpath
8 def bboxrect(cmd):
9 return cmd.bbox().rect()
11 def dotest(c, x, y, test):
12 c2 = c.insert(canvas.canvas([trafo.translate(x, y)]))
13 eval("%s(c2)" % test)
14 c.stroke(bboxrect(c2))
16 class cross(path):
17 def __init__(self, x, y):
18 path.__init__(self, moveto(x,y),
19 rmoveto(-0.1, -0.1),
20 rlineto(0.2, 0.2),
21 rmoveto(-0.1, -0.1),
22 rmoveto(-0.1, +0.1),
23 rlineto(0.2, -0.2))
26 def testarcs(c):
27 def testarc(c, x, y, phi1, phi2):
28 p=path(arc(x,y, 0.5, phi1, phi2))
29 np = p.normpath()
30 c.stroke(p, [color.rgb.red])
31 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
33 def testarcn(c, x, y, phi1, phi2):
34 p=path(arcn(x,y, 0.5, phi1, phi2))
35 np = p.normpath()
36 c.stroke(p, [color.rgb.red])
37 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
39 def testarct(c, r, x0, y0, dx1, dy1, dx2, dy2):
40 p=path(moveto(x0,y0), arct(x0+dx1,y0+dy1, x0+dx2, y0+dy2, r), rlineto(dx2-dx1, dy2-dy1), closepath())
41 np = p.normpath()
42 c.stroke(p, [color.rgb.red, style.linewidth.Thick])
43 c.stroke(np, [color.rgb.green, style.linewidth.THin, deco.filled([color.rgb.green])])
45 testarc(c, 1, 2, 0, 90)
46 testarc(c, 2, 2, -90, 90)
47 testarc(c, 3, 2, 270, 90)
48 testarc(c, 4, 2, 90, -90)
49 testarc(c, 5, 2, 90, 270)
50 testarc(c, 4, 3, 45, -90)
51 testarc(c, 2, 3, 45, -90-2*360)
52 testarc(c, 1, 3, 45, +90+2*360)
54 testarcn(c, 1, 5, 0, 90)
55 testarcn(c, 2, 5, -90, 90)
56 testarcn(c, 3, 5, 270, 90)
57 testarcn(c, 4, 5, 90, -90)
58 testarcn(c, 5, 5, 90, 270)
59 testarcn(c, 4, 6, 45, -90)
60 testarcn(c, 2, 6, 45, -90-360)
61 testarcn(c, 1, 6, 45, -90+360)
64 x0 = -5
65 for angle in [-45, 0, 45, 90, 135, 180, 225, 315]:
66 angle_rad = angle*pi/180
67 testarct(c, 0.1, x0, 0, 0, 1, 2*cos(angle_rad), 2*sin(angle_rad))
68 x0 += 2
70 testarct(c, 0.5, 1, 8, 0, 1, 1, 1)
71 testarct(c, 0.5, 3, 8, 1, 1, 1, 2)
72 testarct(c, 0.5, 5, 8, 1, 0, 2, 1)
73 testarct(c, 0.5, 7, 8, 1, 0, 2, 0)
74 testarct(c, 0.0, 9, 8, 0, 1, 1, 1)
76 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
79 def testintersectbezier(c):
80 p=path(moveto(0,0), curveto(2,6,4,5,2,9)).normpath(epsilon=1e-4)
81 q=path(moveto(2,0), curveto(2,6,4,12,1,6)).normpath(epsilon=1e-4)
83 c.stroke(q, [style.linewidth.THIN])
84 c.stroke(p, [style.linewidth.THIN])
86 isect = p.intersect(q)
88 for i in isect[0]:
89 x, y = p.at(i)
90 c.stroke(cross(x, y), [style.linewidth.THIN])
92 def testintersectcircle(c):
93 k=circle(0, 0, 2)
94 l=line(0, 0, 3, 0)
95 c.stroke(k, [style.linewidth.THIN])
96 c.stroke(l, [style.linewidth.THIN])
98 isect = k.intersect(l)
99 assert len(isect[0])==1, "double count of intersections"
101 for i in isect[0]:
102 x, y = k.at(i)
103 c.stroke(cross(x, y), [style.linewidth.THIN])
105 def testintersectline(c):
106 l1=line(0, 0, 1, 1)
107 l2=line(0, 1, 1, 0)
108 c.stroke(l1, [style.linewidth.THIN])
109 c.stroke(l2, [style.linewidth.THIN])
111 isect = l1.intersect(l2)
113 for i in isect[0]:
114 x, y = l1.at(i)
115 c.stroke(circle(x, y, 0.1), [style.linewidth.THIN])
117 l1=curve(0, 0, 0, 0, 0, 0, 1, 1)
118 c.stroke(l1, [style.linewidth.THIN])
120 isect = l1.intersect(l2)
122 for i in isect[0]:
123 x, y = l1.at(i)
124 c.stroke(circle(x, y, 0.1), [style.linewidth.THIN])
127 def testnormpathtrafo(c):
128 p = path(moveto(0, 5),
129 curveto(2, 1, 4, 0, 2, 4),
130 rcurveto(-3, 2, 1, 2, 3, 6),
131 rlineto(0, 3),
132 closepath())
134 c.stroke(p)
135 c.stroke(p.normpath(), [color.rgb.green, style.linestyle.dashed])
136 c.stroke(p, [trafo.translate(3, 1), color.rgb.red])
137 c.insert(canvas.canvas([trafo.translate(3,1)])).stroke(p,
138 [color.rgb.green,
139 style.linestyle.dashed])
141 c.stroke(p.reversed(), [color.rgb.blue, style.linestyle.dotted, style.linewidth.THick])
143 c.stroke(cross(*(p.at(0))))
144 c.stroke(cross(*(p.reversed().at(0))))
146 p1, p2 = p.split([1.0, 2.1])
147 c.stroke(p1, [color.rgb.red, style.linestyle.dashed])
148 c.stroke(p2, [color.rgb.green, style.linestyle.dashed])
150 circ1 = circle(0, 10, 1)
151 circ2 = circle(1.7, 10, 1)
153 c.stroke(circ1)
154 c.stroke(circ2)
156 isectcirc1, isectcirc2 = circ1.intersect(circ2)
157 segment1 = circ1.split(isectcirc1)[0]
158 segment2 = circ2.split(isectcirc2)[1]
160 segment = segment1 << segment2
161 segment[-1].close()
163 c.stroke(segment, [style.linewidth.THick, deco.filled([color.rgb.green])])
165 def testtangent(c):
166 p=path(moveto(0,5),
167 curveto(2,1,4,0,2,4),
168 rcurveto(-3,2,1,2,3,6),
169 rlineto(2,3)) + circle(5,5,1)
170 c.stroke(p, [style.linewidth.THick])
171 arclen = p.arclen()
172 points = 20
173 for i in range(points):
174 c.stroke(p.tangent(arclen*i/points, length=20*unit.t_pt), [color.rgb.blue, deco.earrow.normal])
175 c.stroke(line(0, 0, 1, 0).transformed(p.trafo(arclen*i/points)), [color.rgb.green, deco.earrow.normal])
176 c.stroke(line(0, 0, 0, 1).transformed(p.trafo(arclen*i/points)), [color.rgb.red, deco.earrow.normal])
178 # test the curvature
179 cc = canvas.canvas()
180 cc.stroke(p)
181 cc = canvas.canvas([canvas.clip(cc.bbox().path())])
182 for i in range(points):
183 radius = p.curveradius(arclen*i/points)
184 if radius is not normpath.invalid:
185 radius = unit.tocm(radius)
186 pos = p.trafo(arclen*i/points).apply(0,radius*radius/abs(radius))
187 cc.stroke(circle(0, 0,unit.t_cm * abs(radius)), [color.grey(0.5), trafo.translate(*pos)])
188 c.insert(cc)
191 def testarclentoparam(c):
192 curve=path(moveto(0,0), lineto(0,5), curveto(5,0,0,10,5,5), closepath(),
193 moveto(5,0), lineto(10,5))
194 ll = curve.arclen()
195 # l=[-0.8*ll, -0.6*ll, -0.4*ll, -0.2*ll, 0, 0.1*ll, 0.3*ll, 0.5*ll, 0.7*ll, 0.9*ll]
196 l=[0, 0.1*ll, 0.2*ll, 0.3*ll, 0.4*ll, 0.5*ll, 0.6*ll, 0.7*ll, 0.8*ll, 0.9*ll]
197 cols=[color.gray.black, color.gray(0.3), color.gray(0.7), color.rgb.red,
198 color.rgb.green, color.rgb.blue, color.cmyk(1,0,0,0),
199 color.cmyk(0,1,0,0), color.cmyk(0,0,1,0), color.gray.black]
200 t=curve.arclentoparam(l)
201 c.stroke(curve)
202 for i in range(len(t)):
203 c.draw(circle(curve.at(t[i])[0], curve.at(t[i])[1], 0.1), [deco.filled([cols[i]]), deco.stroked()])
206 c = canvas.canvas()
207 dotest(c, 0, 0, "testarcs")
208 dotest(c, 2, 12, "testintersectbezier")
209 dotest(c, 10,11, "testnormpathtrafo")
210 dotest(c, 12, -4, "testtangent")
211 dotest(c, 5, -4, "testintersectcircle")
212 dotest(c, 9, -4, "testintersectline")
213 dotest(c, 21, 12, "testarclentoparam")
214 c.writeEPSfile("test_path", paperformat=document.paperformat.A4, rotated=0, fittosize=1)
215 c.writePDFfile("test_path")