简体   繁体   English

获取点到几何的距离

[英]Get the distance from a Point to a Geometry

I have System.Windows.Media.Geometry g and System.Windows.Point p . 我有System.Windows.Media.Geometry gSystem.Windows.Point p

在此处输入图片说明

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. Geometry.GetOutlinedPathGeometry()返回PathGeometry。

  2. PathGeometry.Figures returns PathFigure. PathGeometry.Figures返回PathFigure。

  3. PathFigure.Segments returns PathSegment. PathFigure.Segments返回PathSegment。

  4. No useful method on PathSegment... 在PathSegment上没有有用的方法...

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: 在SO上有一篇文章可以找到最接近隔离点的点:

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

And there's a C++ algorithm that does the distance measuring. 并且有一个C ++算法可以进行距离测量。 You'll just have to convert it to C#: 您只需要将其转换为C#:

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

You can probably also use Point.Subtract() method to get and compare Vectors between each of those points. 您可能还可以使用Point.Subtract()方法来获取和比较这些点之间的Vectors

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. 我建议在第二个链接中使用C ++算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM