作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 SVG 文件,例如,这个
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" />
</svg>
如何获取这些路径的点 (x, y) 列表?
最佳答案
您可以在此处更改点的比例、偏移和密度:
from svg.path import parse_path
from xml.dom import minidom
def get_point_at(path, distance, scale, offset):
pos = path.point(distance)
pos += offset
pos *= scale
return pos.real, pos.imag
def points_from_path(path, density, scale, offset):
step = int(path.length() * density)
last_step = step - 1
if last_step == 0:
yield get_point_at(path, 0, scale, offset)
return
for distance in range(step):
yield get_point_at(
path, distance / last_step, scale, offset)
def points_from_doc(doc, density=5, scale=1, offset=0):
offset = offset[0] + offset[1] * 1j
points = []
for element in doc.getElementsByTagName("path"):
for path in parse_path(element.getAttribute("d")):
points.extend(points_from_path(
path, density, scale, offset))
return points
string = """<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" />
</svg>"""
doc = minidom.parseString(string)
points = points_from_doc(doc, density=1, scale=5, offset=(0, 5))
doc.unlink()
您还可以将这些点可视化:
import pygame
from svg.path import parse_path
from xml.dom import minidom
... # other functions and string
def main():
screen = pygame.display.set_mode([500, 500])
screen.fill((255, 255, 255))
doc = minidom.parseString(string)
points = points_from_doc(doc, 0.05, 5, (0, 5))
doc.unlink()
for point in points:
pygame.draw.circle(screen, (0, 0, 255), point, 1)
pygame.display.flip()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pygame.init()
main()
pygame.quit()
density == 0.05
:
density == 0.1
:
density == 0.5
:
density == 1
:
density == 5
:
关于python - 如何获得 svg 路径的点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69313876/
我有一个问题需要分而治之解决。有一个包含 N 个点的集合 S。如果有一个平行于轴的正方形,只包含S中的两个点p1和p2,则我们称p1和p2为 friend 点。 现在,我需要使用分而治之算法来计算 S
为 iPad 编程时,字体(和其他)大小以“磅”为单位指定。我已经看到将点作为独立于屏幕分辨率的像素的引用。但是我无法确定一个点的实际大小(即以英寸为单位)。一个点是否等于标准 iPad 屏幕上的一个
我有一个来自 Hadley Wickham 的 ggplot2 书中的问题。 我在这里有这个数据框: class % group_by(class) %>% summarise(n = n
好的,这是一些代码( pdfDocument 是 com.itextpdf.text.Document ): PdfPTable table = new PdfPTable(1); PdfPCell
我正在尝试添加一个 if 语句,如果小于 17,则将另一张牌添加到 DealerHand 中。 目前,它只是记录: 7 19 [ { suit: '♦', value: 9, points: 9 },
我正在编写一个程序,我需要: 对图像的每个像素进行测试 如果测试结果为真,我必须向点云中添加一个点 如果测试结果为假,什么都不做 我已经在 CPU 端 C++ 上编写了一个工作代码。现在我需要使用 C
我是一名优秀的程序员,十分优秀!