3

我想放置一个中心在某个位置的倾斜矩形,但我无法正确管理 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()

有什么提示吗?谢谢!

4

0 回答 0