获取 WPF 路径的长度 [英] To get length of WPF path

查看:53
本文介绍了获取 WPF 路径的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PathGeometry 画了一条线.参考 Getting Geometry length我使用GetFlattenedPathGeometry方法得到路径的长度,它将路径转换为一系列直线,并将线长相加.引用的代码是

I have drawn a line using PathGeometry.With the reference from Getting Geometry length I get the length of path using GetFlattenedPathGeometry method, which will convert path to a series of straight lines, and add up the line lengths.The referred code is

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 长度吗??

Is there any other better way to get PathGeometry length??

推荐答案

你可以试试这样的:

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;

}

这篇关于获取 WPF 路径的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆