Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Get single points of a Path?

I have a Path in WPF and I'd like to get the single points of this path. Is this somehow possible? (I used a WPF built-in PathSegment and I'd like to get the points that WPF calculated)

Thanks for any hint!

like image 797
stefan.at.wpf Avatar asked Dec 12 '22 22:12

stefan.at.wpf


2 Answers

Geometry.GetFlattenedPathGeometry returns "a polygonal approximation of the Geometry object." You can then iterate over the figures and segments of the flattened Geometry: each figure should consist of a single PolyLineSegment, from which you can iterate over the Points property to get the points along the path. Thus:

  PathGeometry g = Path.Data.GetFlattenedPathGeometry();

  foreach (var f in g.Figures)
    foreach (var s in f.Segments)
      if (s is PolyLineSegment)
        foreach (var pt in ((PolyLineSegment)s).Points)
          Debug.WriteLine(pt);
like image 75
itowlson Avatar answered Dec 16 '22 17:12

itowlson


In WPF4 there is also the method GetPointAtFractionLength, which lets you get the coordinates of any point and its tangent vector along the length of the path ranging from 0.0 - 1.0.

Very handy to "sample" an arbitrary amount of points along a path.

like image 22
WhiteN01se Avatar answered Dec 16 '22 17:12

WhiteN01se