0

Is there a way to find the Point that is x% along a Path geometry in UWP apps? In WPF this was possible using:

var path = new Path { Data = Geometry.Parse(svgLikePathString) };
var flattened = path.Data.GetFlattenedGeometry();

// e.g. x = 0.5 for 50% along the path
flattened.GetPointAtFractionLength(x, out Point point, out Point tangent);

Context I'm converting a WPF based application to UWP, where I can display different paths from the same array of points, a bit like this https://bl.ocks.org/mbostock/4342190. In UWP I can use this approach to replace Geometry.Parse(string), however path.Data.GetFlattenedGeometry() and GetPointAtFractionLength() no longer exist on Geometry.

will-hart
  • 3,742
  • 2
  • 38
  • 48
  • UWP doesn't support `GetFlattenedGeometry()` and `GetPointAtFractionLength()`. You could think about converting your WPF application to UWP by [Desktop Bridge](https://learn.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-root) directly. – Xie Steven Oct 11 '17 at 01:57
  • Thanks. Yes at this stage I have managed to get a working solution using SkiaSharp for the path operations. I think that will have to be my solution. – will-hart Oct 11 '17 at 06:28

1 Answers1

0

For some reason Path inspection functionality has been removed on the UWP. I've managed to cobble together a solution using SkiaSharp (I also considered Win2D, however SkiaSharp supports more platforms).

In my path generating code, where I previously used the GetFlattenedGeometry I do the following:

// using SkiaSharp;

var path = SKPath.ParseSvgPathData(mySvgLikePathString);
var measure = SKPathMeasure(path);    
var pathLength = measure.Length;

// get the position and tangent half way along the path
measure.GetPositionAndTangent(0.5 * pathLength, out SKPoint pos, out SKPoint tangent);

// do some magic to generate the SVG-like path in here and return the string representation
return GenerateSuperSpecialPath(path, pos, tangent); 

Once I have the path string I can put it in my ViewModel and bind it in the template:

<Path Data={Binding MyPathString, Converter={StaticResource StringToGeometryConverter}} />

The converter looks like this:

public class StringToGeometryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var pathStr = "<Geometry xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" + (string)value + "</Geometry>";
        return (Geometry)XamlReader.Load(pathStr);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new InvalidOperationException("Cannot convert from Geometry to string");
    }
}

Possibly not the most performant solution but it works and I don't notice any serious delays when drawing the paths.

will-hart
  • 3,742
  • 2
  • 38
  • 48