3

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!

igmar
  • 41
  • 5
  • If I understand you correctly you want to draw an array (nx,ny) slanted rectangles. For that you have to: draw the rectangle, slant it using svg skewX() function (see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform), translate it to the desired position then add it to the group. – Daniel Voina Jan 09 '21 at 08:27
  • Is this https://imgur.com/a/6DvGBlr similar to what you want? – Daniel Voina Jan 09 '21 at 08:47
  • The rectangles are not skewed, simply rotated. The problem is that in svgwriter it seems difficult to set combinations of rotations and translations. I will try to follow your suggestion, using the svg internal attributes, even this is not a direct solution to my problem. Thank you – igmar Jan 09 '21 at 11:18

0 Answers0