将路径转换为多边形 [英] Convert path to polygon
问题描述
我正在尝试使用Python来确定一个点是否在SVG路径中.
我将使用的算法是射线广播算法.
I'm trying to figure if a point is in a SVG path using Python.
The algorithm that I'll use is the raycasting algorithm.
但是对于这种算法,我需要具有多边形边,但是我所拥有的只是svg路径的路径数据:
But for this algorithm i need to have the polygon sides, but all I have is the the pathdata for the svg path:
<path
d="m 362.26878,978.51017 c 20.15947,-20.15479 23.0826,-25.35876
20.51836,-36.58367 -5.62899,-24.66928 -8.85902,-84.94939
-4.6845,-87.51832 2.29504,-1.43086 25.27371,2.13445 51.0669,7.87678
39.48315,8.80707 50.0611,13.213 66.91495,27.88988 11.39966,9.91685
25.01402,17.41113 31.62525,17.41113 12.91547,0 24.69288,-11.04544
19.95645,-18.71919 -1.68587,-2.73893 4.50508,-38.63785 13.76077,-79.78795
12.41964,-55.21781 16.82552,-85.81829 16.82552,-116.84379 0,-23.12039 … z" />
与此图片相对应的
that corresponds to this image:
那么,有没有办法获得路径的两侧?
So, is there a way to get the sides of a path?
推荐答案
您可以将路径转换为多边形,如我的示例所示:
http://phrogz.net/svg/convert_path_to_polygon.xhtml
You can convert a path into a polygon as shown in my example here:
http://phrogz.net/svg/convert_path_to_polygon.xhtml
该页面(使用JavaScript)中最简单的算法是:
The simpler algorithm from that page (in JavaScript) is:
function polygonSampledFromPath(path,samples){
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');
var points = [];
var len = path.getTotalLength();
var step = step=len/samples;
for (var i=0;i<=len;i+=step){
var p = path.getPointAtLength(i);
points.push( p.x+','+p.y );
}
poly.setAttribute('points',points.join(' '));
return poly;
}
您可能希望仅在特定距离进行采样,而不是基于一定数量的点进行采样.
这假定您的Python绑定有权访问完整的 SVGPathElement
DOM接口.
This assumes that your Python binding has access to the full SVGPathElement
DOM Interface.
这篇关于将路径转换为多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!