2 import sys
; sys
.path
[:0] = ["../.."]
6 from pyx
import normpath
9 return cmd
.bbox().rect()
11 def dotest(c
, x
, y
, test
):
12 c2
= c
.insert(canvas
.canvas([trafo
.translate(x
, y
)]))
14 c
.stroke(bboxrect(c2
))
17 def __init__(self
, x
, y
):
18 path
.__init
__(self
, moveto(x
,y
),
27 def testarc(c
, x
, y
, phi1
, phi2
):
28 p
=path(arc(x
,y
, 0.5, phi1
, phi2
))
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
))
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())
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)
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
))
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
)
90 c
.stroke(cross(x
, y
), [style
.linewidth
.THIN
])
92 def testintersectcircle(c
):
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"
103 c
.stroke(cross(x
, y
), [style
.linewidth
.THIN
])
105 def testintersectline(c
):
108 c
.stroke(l1
, [style
.linewidth
.THIN
])
109 c
.stroke(l2
, [style
.linewidth
.THIN
])
111 isect
= l1
.intersect(l2
)
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
)
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),
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
,
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)
156 isectcirc1
, isectcirc2
= circ1
.intersect(circ2
)
157 segment1
= circ1
.split(isectcirc1
)[0]
158 segment2
= circ2
.split(isectcirc2
)[1]
160 segment
= segment1
<< segment2
163 c
.stroke(segment
, [style
.linewidth
.THick
, deco
.filled([color
.rgb
.green
])])
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
])
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
])
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
)])
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))
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
)
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()])
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", page_paperformat
=document
.paperformat
.A4
, page_rotated
=0, page_fittosize
=1)
215 c
.writePDFfile("test_path")