2 import sys
; sys
.path
[:0] = ["../.."]
8 return cmd
.bbox().rect()
10 def dotest(c
, x
, y
, test
):
11 c2
= c
.insert(canvas
.canvas([trafo
.translate(x
, y
)]))
13 c
.stroke(bboxrect(c2
))
16 def __init__(self
, x
, y
):
17 path
.__init
__(self
, moveto(x
,y
),
26 def testarc(c
, x
, y
, phi1
, phi2
):
27 p
=path(arc(x
,y
, 0.5, phi1
, phi2
))
29 c
.stroke(p
, [color
.rgb
.red
])
30 c
.stroke(np
, [color
.rgb
.green
, style
.linestyle
.dashed
])
32 def testarcn(c
, x
, y
, phi1
, phi2
):
33 p
=path(arcn(x
,y
, 0.5, phi1
, phi2
))
35 c
.stroke(p
, [color
.rgb
.red
])
36 c
.stroke(np
, [color
.rgb
.green
, style
.linestyle
.dashed
])
38 def testarct(c
, r
, x0
, y0
, dx1
, dy1
, dx2
, dy2
):
39 p
=path(moveto(x0
,y0
), arct(x0
+dx1
,y0
+dy1
, x0
+dx2
, y0
+dy2
, r
), rlineto(dx2
-dx1
, dy2
-dy1
), closepath())
41 c
.stroke(p
, [color
.rgb
.red
, style
.linewidth
.Thick
])
42 c
.stroke(np
, [color
.rgb
.green
, style
.linewidth
.THin
, deco
.filled([color
.rgb
.green
])])
44 testarc(c
, 1, 2, 0, 90)
45 testarc(c
, 2, 2, -90, 90)
46 testarc(c
, 3, 2, 270, 90)
47 testarc(c
, 4, 2, 90, -90)
48 testarc(c
, 5, 2, 90, 270)
49 testarc(c
, 4, 3, 45, -90)
50 testarc(c
, 2, 3, 45, -90-2*360)
51 testarc(c
, 1, 3, 45, +90+2*360)
53 testarcn(c
, 1, 5, 0, 90)
54 testarcn(c
, 2, 5, -90, 90)
55 testarcn(c
, 3, 5, 270, 90)
56 testarcn(c
, 4, 5, 90, -90)
57 testarcn(c
, 5, 5, 90, 270)
58 testarcn(c
, 4, 6, 45, -90)
59 testarcn(c
, 2, 6, 45, -90-360)
60 testarcn(c
, 1, 6, 45, -90+360)
64 for angle
in [-45, 0, 45, 90, 135, 180, 225, 315]:
65 angle_rad
= angle
*pi
/180
66 testarct(c
, 0.1, x0
, 0, 0, 1, 2*cos(angle_rad
), 2*sin(angle_rad
))
69 testarct(c
, 0.5, 1, 8, 0, 1, 1, 1)
70 testarct(c
, 0.5, 3, 8, 1, 1, 1, 2)
71 testarct(c
, 0.5, 5, 8, 1, 0, 2, 1)
72 testarct(c
, 0.5, 7, 8, 1, 0, 2, 0)
73 testarct(c
, 0.0, 9, 8, 0, 1, 1, 1)
75 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
78 def testintersectbezier(c
):
79 p
=path(moveto(0,0), curveto(2,6,4,5,2,9)).normpath(epsilon
=1e-4)
80 q
=path(moveto(2,0), curveto(2,6,4,12,1,6)).normpath(epsilon
=1e-4)
82 c
.stroke(q
, [style
.linewidth
.THIN
])
83 c
.stroke(p
, [style
.linewidth
.THIN
])
85 isect
= p
.intersect(q
)
89 c
.stroke(cross(x
, y
), [style
.linewidth
.THIN
])
91 def testintersectcircle(c
):
94 c
.stroke(k
, [style
.linewidth
.THIN
])
95 c
.stroke(l
, [style
.linewidth
.THIN
])
97 isect
= k
.intersect(l
)
98 assert len(isect
[0])==1, "double count of intersections"
102 c
.stroke(cross(x
, y
), [style
.linewidth
.THIN
])
104 def testintersectline(c
):
107 c
.stroke(l1
, [style
.linewidth
.THIN
])
108 c
.stroke(l2
, [style
.linewidth
.THIN
])
110 isect
= l1
.intersect(l2
)
114 c
.stroke(circle(x
, y
, 0.1), [style
.linewidth
.THIN
])
117 def testnormpathtrafo(c
):
118 p
= path(moveto(0, 5),
119 curveto(2, 1, 4, 0, 2, 4),
120 rcurveto(-3, 2, 1, 2, 3, 6),
125 c
.stroke(p
.normpath(), [color
.rgb
.green
, style
.linestyle
.dashed
])
126 c
.stroke(p
, [trafo
.translate(3, 1), color
.rgb
.red
])
127 c
.insert(canvas
.canvas([trafo
.translate(3,1)])).stroke(p
,
129 style
.linestyle
.dashed
])
131 c
.stroke(p
.reversed(), [color
.rgb
.blue
, style
.linestyle
.dotted
, style
.linewidth
.THick
])
133 c
.stroke(cross(*(p
.at(0))))
134 c
.stroke(cross(*(p
.reversed().at(0))))
136 p1
, p2
= p
.split([1.0, 2.1])
137 c
.stroke(p1
, [color
.rgb
.red
, style
.linestyle
.dashed
])
138 c
.stroke(p2
, [color
.rgb
.green
, style
.linestyle
.dashed
])
140 circ1
= circle(0, 10, 1)
141 circ2
= circle(1.7, 10, 1)
146 isectcirc1
, isectcirc2
= circ1
.intersect(circ2
)
147 segment1
= circ1
.split(isectcirc1
)[0]
148 segment2
= circ2
.split(isectcirc2
)[1]
150 segment
= segment1
<< segment2
153 c
.stroke(segment
, [style
.linewidth
.THick
, deco
.filled([color
.rgb
.green
])])
157 curveto(2,1,4,0,2,4),
158 rcurveto(-3,2,1,2,3,6),
159 rlineto(2,3)) + circle(5,5,1)
160 c
.stroke(p
, [style
.linewidth
.THick
])
163 for i
in range(points
):
164 c
.stroke(p
.tangent(arclen
*i
/points
, length
=20*unit
.t_pt
), [color
.rgb
.blue
, deco
.earrow
.normal
])
165 c
.stroke(line(0, 0, 1, 0).transformed(p
.trafo(arclen
*i
/points
)), [color
.rgb
.green
, deco
.earrow
.normal
])
166 c
.stroke(line(0, 0, 0, 1).transformed(p
.trafo(arclen
*i
/points
)), [color
.rgb
.red
, deco
.earrow
.normal
])
171 cc
= canvas
.canvas([canvas
.clip(cc
.bbox().path())])
172 for i
in range(points
):
173 radius
= p
.curveradius(arclen
*i
/points
)
174 if radius
is not None:
175 radius
= unit
.tocm(radius
)
176 pos
= p
.trafo(arclen
*i
/points
).apply(0,radius
*radius
/abs(radius
))
177 cc
.stroke(circle(0, 0,unit
.t_cm
* abs(radius
)), [color
.grey(0.5), trafo
.translate(*pos
)])
181 def testarclentoparam(c
):
182 curve
=path(moveto(0,0), lineto(0,5), curveto(5,0,0,10,5,5), closepath(),
183 moveto(5,0), lineto(10,5))
185 # 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]
186 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
]
187 cols
=[color
.gray
.black
, color
.gray(0.3), color
.gray(0.7), color
.rgb
.red
,
188 color
.rgb
.green
, color
.rgb
.blue
, color
.cmyk(1,0,0,0),
189 color
.cmyk(0,1,0,0), color
.cmyk(0,0,1,0), color
.gray
.black
]
190 t
=curve
.arclentoparam(l
)
192 for i
in range(len(t
)):
193 c
.draw(circle(curve
.at(t
[i
])[0], curve
.at(t
[i
])[1], 0.1), [deco
.filled([cols
[i
]]), deco
.stroked()])
197 dotest(c
, 0, 0, "testarcs")
198 dotest(c
, 2, 12, "testintersectbezier")
199 dotest(c
, 10,11, "testnormpathtrafo")
200 dotest(c
, 12, -4, "testtangent")
201 dotest(c
, 5, -4, "testintersectcircle")
202 dotest(c
, 9, -4, "testintersectline")
203 dotest(c
, 21, 12, "testarclentoparam")
204 c
.writeEPSfile("test_path", paperformat
=document
.paperformat
.A4
, rotated
=0, fittosize
=1)
205 c
.writePDFfile("test_path")