I would like to place a slanted rectangle with its center in a certain position, but I'm not able to properly manage the rotate and translate functions in svgwrite objects: the final position of the rectangle is not what expected (by me). For example, in the following code, I would like to place each slanted rectangle centered on each coloured rectangle on the backgroud:
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()
Any hints? Thank you!