我使用PathGeometry绘制了一条线.使用
Getting Geometry length的引用,我使用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转换为具有
ComputeLength功能的SharpDX.Direct2D1.Geometry:
Calculates the length of the geometry as though each segment were
unrolled into a line.