Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get length of WPF path

Tags:

c#

wpf

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

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

like image 530
Swarna Latha Avatar asked Nov 02 '22 05:11

Swarna Latha


1 Answers

With little effort You can convert System.Windows.Media.Geometry to SharpDX.Direct2D1.Geometry that have ComputeLength function:

Calculates the length of the geometry as though each segment were unrolled into a line.

like image 65
google dev Avatar answered Nov 08 '22 11:11

google dev