2

I have System.Windows.Media.Geometry g and System.Windows.Point p.

enter image description here

I want to know the shortest distance between the point and the outline of the geometry. How should I do it?

Here is my effort:

  1. Geometry.GetOutlinedPathGeometry() returns PathGeometry.

  2. PathGeometry.Figures returns PathFigure.

  3. PathFigure.Segments returns PathSegment.

  4. No useful method on PathSegment...

Gqqnbig
  • 5,845
  • 10
  • 45
  • 86
  • I read MSDN and foud `GetOutlinedPathGeometry` returns the outline of type `PathGeometry`. But I don't find any useful method on PathGeometry. – Gqqnbig May 14 '14 at 06:15
  • 1
    As a completely off topic comment, do you really need to get such a precise distance? Distance from the containing box wouldn't be sufficient? – Kilazur May 14 '14 at 09:19

1 Answers1

2

Basically, what you need to do is go through every point on that path that you got from your geometry and measure distance between one of those points and the segregated point.

There's a post on SO that finds the closest point to the segregated point:

https://stackoverflow.com/a/19031758/2006048

And there's a C++ algorithm that does the distance measuring. You'll just have to convert it to C#:

https://stackoverflow.com/a/1501725/2006048

You can probably also use Point.Subtract() method to get and compare Vectors between each of those points.

If you can get an array or list of points from that shape, then you can probably do something like this (Note that this is not as elaborate as the links I've provided. This will give you the shortest distance to one of the available points and not the segment itself):

public static double GetShortestDistance(Point target, Point[] points)
{
    var dist = 0;
    foreach (var point in points)
    {
        var xDist = target.X - point.X;
        var yDist = target.Y - point.Y;
        var nDist = Math.Sqrt(xDist * xDist + yDist * yDist);

        if (nDist < dist)
        {
            dist = nDist;
        }
    }

    return dist;
}

I recommend using the C++ algorithm in the second link.

Community
  • 1
  • 1
B.K.
  • 9,982
  • 10
  • 73
  • 105
  • 1
    This is not per definition the shortest path: given target at (0,1) and points at respectively (2,0) and (2,2), the distance should be exactly 2.0. – tofi9 May 14 '14 at 06:58
  • So how do I get path or polyline from `Geometry`? I don't know which API to use. Do I finally need to have `PathSegment`? – Gqqnbig May 14 '14 at 06:59
  • @LoveRight http://stackoverflow.com/questions/3751700/get-a-list-of-coordinates-from-system-windows-media-geometry – tofi9 May 14 '14 at 07:02
  • So if I understand correctly, I need to test if ps is LineSegment, if ps is ArcSegment, if ps is BezierSegment, etc. – Gqqnbig May 14 '14 at 07:09
  • 2
    @taoufik Well, it would be 2.0 directly to the edge, but approximately 2.23 measured to either of those points. I don't know how he can get the shortest path to the edge, rather than the point. The link to the C++ algorithm does a better job than what I've posted. – B.K. May 14 '14 at 07:10