获得几何长度 [英] Getting Geometry length

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

问题描述

有谁知道在Pixels中获取WPF几何体长度的有效方法吗?
我知道WPF中的几何图形是基于矢量的,因此实际上没有像素长度。但必须能够根据可见的绘制图像获取长度。我的意思是,如果我在1024x800像素的图像中绘制几何图形,它必须能够获得这些图像的长度。



我错了吗?还是有任何可能和有效获取这些信息的方式?

预先感谢您!

Michael

解决方案

我已经提出了一个解决方案,但不知道这是否是最有效率(最快)的方式。请您在此留下评论 - 谢谢大家。



 公共静态双GetLength进行(这种几何GEO)
{$! b $ b PathGeometry path = geo.GetFlattenedPathGeometry();

双倍长度= 0.0;

foreach(PathFigure pf in path.Figures)
{
Point start = pf.StartPoint;

的foreach(在pf.Segments PolyLineSegment SEG)
{
的foreach(在seg.Points点点)
{
长度+ =距离(开始,点);
start = point;
}
}
}

返回长度;
}

私有静态双距离(点P1,P2点)
{
返回Math.Sqrt(Math.Pow(p1.X - p2.X, 2)+ Math.Pow(p1.Y - p2.Y,2));
}


Does anyone know an efficient way of getting the length of a WPF geometry in Pixels? I know that geometries in WPF are vector based and therefore do not really have a pixellength. But it has to be possible to get the length based on the visible drawn image. I mean if I draw some geometries in a 1024x800 pixel image it has to be possible to get the length of those.

Am I wrong here or is there any possible and efficient way of getting these informations?

Thank you in advance!

Michael

解决方案

I have come up with a solution, but don't know if this would be the most efficent (fastest) way of doing so. Please leave your comments on this - thank you all!

    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));
    }

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

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