Getting Geometry length

烂漫一生 提交于 2020-01-03 17:27:08

问题


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


回答1:


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



回答2:


var geometry = new EllipseGeometry(new Point(50,50), 45, 20);
var length = geometry.Bounds.Width; //or geometry.Bounds.Height

Note: EllipseGeometry is just an example. All classes that derive from Geometry have a Bounds property.



来源:https://stackoverflow.com/questions/10877631/getting-geometry-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!