弧形,切割,svgwrite

时间:2014-07-29 15:43:47

标签: python svg inkscape

是否有任何身体在svgwrite(python)中有一个圆弧切片(奶酪切片或pac-man)的工作示例,我试过这个试图让NorthWest象限在(100,100)处进行,但得到一个奇怪的形状:

w = dwg.path(d="M 100,50 A 50,50 0 0 1 50,100 l 0,50 z", fill="#ffff00", stroke='none')

我查看了inkscape生成的svg的实际输出,这里是xml等价物:

<path
   d="M 100,50 A 50,50 0 0 1 50,100 l 0,50 z"
   style="fill:#ffff00;fill-opacity:1;fill-rule:nonzero;stroke:none"
   sodipodi:type="arc"
   sodipodi:cx="100"
   sodipodi:cy="100"
   sodipodi:rx="50"
   sodipodi:ry="50"
   sodipodi:start="4.71"
   sodipodi:end="3.14"
    />

但是我无法a)获取svgwrite来生成这个b)如果我把svgwrite生成的svg文件替换成它的代码片段,它就没有效果,可能是因为sodipodi命令是特定于inkscape的。

任何人都有这方面的实例吗?

2 个答案:

答案 0 :(得分:3)

在阅读更多文档并查看示例

下的链接后,确定

http://www.w3.org/TR/SVG11/paths.html#PathData

这是svgwrite

中工作楔的一个例子
    w = dwg.path(d="M0,0 v-150 a150,150 0 0,0 -150,150 z",
             fill="#00ff00", 
             stroke="none",
            )

这给了我一个西北象限。

我想把我的圈子分成10度切口,所以我写了这段代码:

start_x = 250
start_y = 300
radius = 100
for i in range(36):
    degree0 = 0 + i*10
    degree1 = 10 + i*10
    radians0 = math.radians(degree0)
    radians1 = math.radians(degree1)
    dx0 = radius*(math.sin(radians0))
    dy0 = radius*(math.cos(radians0))
    dx1 = radius*(math.sin(radians1))
    dy1 = radius*(math.cos(radians1))

    m0 = dy0 
    n0 = -dx0 
    m1 = -dy0 + dy1 
    n1 = dx0 - dx1 

    w = dwg.path(d="M {0},{1} l {2},{3} a {4},{4} 0 0,0 {5},{6} z".format(start_x, start_y, m0, n0, radius, m1, n1),
             fill="#00ff00", 
             stroke="none",
            )
    elem.append(w)

如果你想运行三角学,那么明确地给了我所有这些切割,也可以明确地使用。这是针对以(250,300)为中心的半径为100的分段。

0   : d=" M 250,300 l 100.0,-0.0 a 100,100 0 0,0 -1.5192246987791975,-17.364817766693033 z "
10  : d=" M 250,300 l 98.4807753012208,-17.364817766693033 a 100,100 0 0,0 -4.511513222629958,-16.837196565873835 z "
20  : d=" M 250,300 l 93.96926207859084,-34.20201433256687 a 100,100 0 0,0 -7.366721700146968,-15.797985667433124 z "
30  : d=" M 250,300 l 86.60254037844388,-49.99999999999999 a 100,100 0 0,0 -9.998096066546069,-14.278760968653934 z "
40  : d=" M 250,300 l 76.60444431189781,-64.27876096865393 a 100,100 0 0,0 -12.325683343243867,-12.325683343243881 z "
50  : d=" M 250,300 l 64.27876096865394,-76.60444431189781 a 100,100 0 0,0 -14.278760968653927,-9.998096066546054 z "
60  : d=" M 250,300 l 50.000000000000014,-86.60254037844386 a 100,100 0 0,0 -15.797985667433132,-7.366721700146968 z "
70  : d=" M 250,300 l 34.20201433256688,-93.96926207859083 a 100,100 0 0,0 -16.837196565873843,-4.511513222629972 z "
80  : d=" M 250,300 l 17.36481776669304,-98.4807753012208 a 100,100 0 0,0 -17.364817766693033,-1.5192246987791975 z "
90  : d=" M 250,300 l 6.123233995736766e-15,-100.0 a 100,100 0 0,0 -17.364817766693037,1.5192246987791975 z "
100 : d=" M 250,300 l -17.36481776669303,-98.4807753012208 a 100,100 0 0,0 -16.83719656587384,4.511513222629958 z "
110 : d=" M 250,300 l -34.20201433256687,-93.96926207859084 a 100,100 0 0,0 -15.79798566743311,7.366721700146968 z "
120 : d=" M 250,300 l -49.99999999999998,-86.60254037844388 a 100,100 0 0,0 -14.278760968653962,9.998096066546069 z "
130 : d=" M 250,300 l -64.27876096865394,-76.60444431189781 a 100,100 0 0,0 -12.325683343243853,12.325683343243867 z "
140 : d=" M 250,300 l -76.6044443118978,-64.27876096865394 a 100,100 0 0,0 -9.998096066546083,14.278760968653948 z "
150 : d=" M 250,300 l -86.60254037844388,-49.99999999999999 a 100,100 0 0,0 -7.366721700146954,15.797985667433103 z "
160 : d=" M 250,300 l -93.96926207859083,-34.20201433256689 a 100,100 0 0,0 -4.511513222629972,16.837196565873864 z "
170 : d=" M 250,300 l -98.4807753012208,-17.364817766693026 a 100,100 0 0,0 -1.5192246987791975,17.364817766693015 z "
180 : d=" M 250,300 l -100.0,-1.2246467991473532e-14 a 100,100 0 0,0 1.5192246987791975,17.364817766693058 z "
190 : d=" M 250,300 l -98.4807753012208,17.364817766693047 a 100,100 0 0,0 4.511513222629958,16.83719656587382 z "
200 : d=" M 250,300 l -93.96926207859084,34.20201433256687 a 100,100 0 0,0 7.366721700146982,15.797985667433146 z "
210 : d=" M 250,300 l -86.60254037844386,50.000000000000014 a 100,100 0 0,0 9.998096066546054,14.278760968653913 z "
220 : d=" M 250,300 l -76.60444431189781,64.27876096865393 a 100,100 0 0,0 12.325683343243867,12.325683343243867 z "
230 : d=" M 250,300 l -64.27876096865394,76.6044443118978 a 100,100 0 0,0 14.278760968653899,9.998096066546054 z "
240 : d=" M 250,300 l -50.00000000000004,86.60254037844385 a 100,100 0 0,0 15.797985667433188,7.3667217001469965 z "
250 : d=" M 250,300 l -34.202014332566854,93.96926207859084 a 100,100 0 0,0 16.83719656587382,4.511513222629958 z "
260 : d=" M 250,300 l -17.364817766693033,98.4807753012208 a 100,100 0 0,0 17.364817766693015,1.5192246987791975 z "
270 : d=" M 250,300 l -1.8369701987210297e-14,100.0 a 100,100 0 0,0 17.364817766693015,-1.5192246987791833 z "
280 : d=" M 250,300 l 17.364817766692997,98.48077530122082 a 100,100 0 0,0 16.8371965658739,-4.511513222629986 z "
290 : d=" M 250,300 l 34.2020143325669,93.96926207859083 a 100,100 0 0,0 15.797985667433117,-7.366721700146968 z "
300 : d=" M 250,300 l 50.000000000000014,86.60254037844386 a 100,100 0 0,0 14.278760968653913,-9.998096066546054 z "
310 : d=" M 250,300 l 64.27876096865393,76.60444431189781 a 100,100 0 0,0 12.325683343243853,-12.325683343243853 z "
320 : d=" M 250,300 l 76.60444431189778,64.27876096865396 a 100,100 0 0,0 9.998096066546054,-14.278760968653913 z "
330 : d=" M 250,300 l 86.60254037844383,50.00000000000004 a 100,100 0 0,0 7.366721700147011,-15.797985667433181 z "
340 : d=" M 250,300 l 93.96926207859084,34.20201433256686 a 100,100 0 0,0 4.511513222629958,-16.83719656587382 z "
350 : d=" M 250,300 l 98.4807753012208,17.36481776669304 a 100,100 0 0,0 1.5192246987791975,-17.364817766693015 z "

答案 1 :(得分:1)

感谢道格拉斯!我把你的弧形绘图答案变成了一个函数,以防它帮助其他人:

def addArc(dwg, current_group, p0, p1, radius):
    """ Adds an arc that bulges to the right as it moves from p0 to p1 """
    args = {'x0':p0[0], 
        'y0':p0[1], 
        'xradius':radius, 
        'yradius':radius, 
        'ellipseRotation':0, #has no effect for circles
        'x1':(p1[0]-p0[0]), 
        'y1':(p1[1]-p0[1])}
    current_group.add(dwg.path(d="M %(x0)f,%(y0)f a %(xradius)f,%(yradius)f %(ellipseRotation)f 0,0 %(x1)f,%(y1)f"%args,
             fill="none", 
             stroke='red', stroke_width=line_stroke_width
            ))


# usage example:
import svgwrite
dwg = svgwrite.Drawing(filename="test.svg", debug=True, size=(4000,1000))
current_group = dwg.add(dwg.g(id=name, stroke='red', stroke_width=3, fill='none', fill_opacity=0 ))
addArc(dwg, current_group, p0=[10,10], p1=[40,10], radius=80)
dwg.save()

显然你可以用dwg.line()跟上上面的弧线绘制函数来绘制饼图片或其他东西。