[英]To get length of WPF path
我使用PathGeometry
绘制了一条线。使用PathGeometry
Geometry长度的引用,我使用GetFlattenedPathGeometry方法得到路径的长度,该方法将路径转换为一系列直线,并累加线长。引用的代码是
public static double GetLength(this Geometry geo)
{
PathGeometry path = geo.GetFlattenedPathGeometry();
double length = 0.0;
foreach (PathFigure pf in path.Figures)
{
Point start = pf.StartPoint;
foreach (PolyLineSegment seg in pf.Segments)
{
foreach (Point point in seg.Points)
{
length += Distance(start, point);
start = point;
}
}
}
return length;
}
private static double Distance(Point p1, Point p2)
{
return Math.Sqrt(Math.Pow(p1.X - p2.X,2) + Math.Pow(p1.Y - p2.Y,2));
}
还有其他更好的方法来获取PathGeometry的长度吗?
很少的努力,你可以转换System.Windows.Media.Geometry
到SharpDX.Direct2D1.Geometry
有ComputeLength功能:
计算几何的长度,就像每个段展开成一条线一样。
你可以尝试这样的事情:
public static double GetLengthOfGeo(Geometry geo)
{
PathGeometry pg = PathGeometry.CreateFromGeometry(geo);
Point p; Point tp;
pg.GetPointAtFractionLength(0.0001, out p, out tp);
return (pg.Figures[0].StartPoint - p).Length*10000;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.