相关疑难解决方法(0)

获取 WPF 路径的长度

我已经使用PathGeometry.With绘制了一条线。从获取几何长度的参考中,我使用 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));
    }
Run Code Online (Sandbox Code Playgroud)

有没有其他更好的方法来获得 PathGeometry 长度?

c# wpf

5
推荐指数
0
解决办法
1369
查看次数

标签 统计

c# ×1

wpf ×1