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??
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With