![](/img/trans.png)
[英]How to get the distance of two point geometry in meters using NetTopologySuite?
[英]Get the distance from a Point to a Geometry
我有System.Windows.Media.Geometry g
和System.Windows.Point p
。
我想知道點與幾何輪廓之間的最短距離。 我該怎么辦?
這是我的努力:
Geometry.GetOutlinedPathGeometry()返回PathGeometry。
PathGeometry.Figures返回PathFigure。
PathFigure.Segments返回PathSegment。
在PathSegment上沒有有用的方法...
基本上,您需要做的是遍歷您從幾何圖形獲得的路徑上的每個點,並測量這些點之一與分離點之間的距離。
在SO上有一篇文章可以找到最接近隔離點的點:
https://stackoverflow.com/a/19031758/2006048
並且有一個C ++算法可以進行距離測量。 您只需要將其轉換為C#:
https://stackoverflow.com/a/1501725/2006048
您可能還可以使用Point.Subtract()
方法來獲取和比較這些點之間的Vectors
。
如果您可以從該形狀中獲取點的數組或列表,則可以執行以下操作(請注意,此操作不如我提供的鏈接那么詳細。這將使您到可用鏈接的距離最短點而不是細分本身):
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;
}
我建議在第二個鏈接中使用C ++算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.