python - 如何在 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()
有什么提示吗?谢谢!
解决方案
推荐阅读
- docker - 如何使 kubernetes docker secret 工作?
- sql-server - 查询 SQL Server 的 bcp 问题
- c++ - assimp,如何保存压缩的 assbin 文件
- spring-mvc - 春天 5
- javascript - FLOW:如何使 Flow 与 Array.prototype.find() 的回调一起工作
- python-3.x - Pytest:如何在测试应用程序时创建模拟套接字服务器以伪造响应
- node.js - 什么会导致节点项目在一台机器上工作但在另一台机器上失败?(两者都使用相同版本的节点)
- javascript - HTML 中的 JavaScript - 无法使用单选按钮值来显示条件结果
- javascript - Ask.fm 节点 api cookie
- python - 将 .profo 文件的文件夹编译为多种语言