如何在 svgwrite 中正确旋转和平移对象

时间:2021-01-09 08:12:48

标签: python svg svgwrite

我想放置一个中心在某个位置的倾斜矩形,但我无法正确管理 svgwrite 对象中的旋转和平移功能:矩形的最终位置不是预期的(由我)。例如,在下面的代码中,我想将每个倾斜的矩形以背景上的每个彩色矩形为中心:

import numpy as np
import random
import svgwrite
svgname = r'test_array.svg'
dwg = svgwrite.Drawing(svgname, profile='tiny')
angle = 13
# Size of rectangles in the basic mosaic
pitch_x = 50 # step
pitch_y = 20 # step
nx = 6 # nr along x
ny = 4 # nr along y
# Size of the slanted rectangles
rect_w = 12
rect_h = 30
# Final group
global_group = dwg.g()
x0 = 0
y0 = 0
for ky in range(ny):
    for kx in range(nx):
        global_group.add(dwg.rect(insert=(x0+kx*pitch_x, y0+ky*pitch_y),size=(pitch_x,pitch_y), fill=svgwrite.rgb(random.random()*100, random.random()*100, random.random()*100, '%'), stroke='', stroke_width=0 ))
for ky in range(ny):
    for kx in range(nx):
        segm = dwg.rect(insert=(0,0),size=(rect_w,rect_h), fill='gray', stroke='', stroke_width=0 )
        segm.rotate(-angle, center=(kx*pitch_x,  ky*pitch_y))
        segm.translate(kx*pitch_x,ky*pitch_y)
        global_group.add(segm)
dwg.add(global_group)
dwg.save()

有什么提示吗? 谢谢!

0 个答案:

没有答案