gpt4 book ai didi

c# - wgs​​ 点与 wgs 定义线段的距离

转载 作者:行者123 更新时间:2023-11-30 15:39:47 27 4
gpt4 key购买 nike

我进行了搜索,但找不到完整的答案。如果可能的话,在 C# 中。我需要 WGS 点和球体上 WGS 点定义的线段之间的最短距离(正好是地球)。

float DistanceInKilometres(PointF LineStartA, PointF LineEndB, PointF ThePoint)

编辑:也许插图会有所帮助

enter image description here

请注意,这是一个理想的例子。 “点”可以是球体表面的任何地方,也可以是线段的起点和终点。显然,我不是在寻找通过球体的距离。数学不是我的强项,所以我不了解归一化笛卡尔。也许我还应该注意到路径 AB 是最短的可能路径,而 Distance? 也是最短的可能路径。

最佳答案

您可以使用余弦的球面定律:

您将不得不使用地球的半径进行计算:

EARTH_RADIUS_KM = 6371;

这里,来 self 对 OsmMercator.java 的贡献,来自 openstreetmap.org:

/**
* Gets the distance using Spherical law of cosines.
*
* @param la1 the Latitude in degrees
* @param lo1 the Longitude in degrees
* @param la2 the Latitude from 2nd coordinate in degrees
* @param lo2 the Longitude from 2nd coordinate in degrees
* @return the distance
*/
public static double getDistance(double la1, double lo1, double la2, double lo2) {
double aStartLat = Math.toRadians(la1);
double aStartLong = Math.toRadians(lo1);
double aEndLat =Math.toRadians(la2);
double aEndLong = Math.toRadians(lo2);

double distance = Math.acos(Math.sin(aStartLat) * Math.sin(aEndLat)
+ Math.cos(aStartLat) * Math.cos(aEndLat)
* Math.cos(aEndLong - aStartLong));

return (EARTH_RADIUS_KM * distance);
}

您需要做的就是使用点积找到最近的点,并将其用于距离方程。

这是最近点的例子:

double[] nearestPointSegment (double[] a, double[] b, double[] c)
{
double[] t= nearestPointGreatCircle(a,b,c);
if (onSegment(a,b,t))
return t;
return (distance(a,c) < distance(b,c)) ? a : c;
}

请记住,单位尚未明确声明。在处理空间中的点时,有多种方法可以确定位置。最主要的是你必须将你的单位确定为一致的类型。

在处理地球上的位置时,我主要使用纬度/经度坐标和矢量来确定大小/方向。有几种已知类型可用于矢量和地球位置。其中包括:

  • 地心地固 (ECEF) 坐标系
  • 东北下 (NED)
  • 大地坐标系

对于您的示例,我可能会考虑坚持使用 Geodetic。

现在,将这些放在一起,您可能会得到一些如下所示的伪代码:

Where a Vector is made up of Geodetic coordinates:
class Vector {
double x=0.0; //latitude
double y=0.0; //longitude
double h=0.0; //height
...
}

public Vector closestPoint(Vector lineStartA, Vector lineEndB, final Vector thePoint ) {
Vector w = thePoint.subtract(lineStartA);
double proj = w.dot(lineEndB);
// endpoint 0 is closest point
if ( proj <= 0.0f )
return lineStartA;
else
{
//Vector square
double vsq = lineEndB.dot(lineEndB);
// endpoint 1 is closest point
if ( proj >= vsq )
return lineStartA.add(lineEndB);
else
return lineStartA.add(lineEndB.multiply(proj/vsq));
}
}

double DistanceInKilometres(Vector lineStartA, Vector lineEndB, Vector thePoint) {
Vector cp=closestPoint(lineStartA, lineEndB, thePoint);
return getDistance(cp.x, cp.y, thePoint.x, thePoint.y);
}

关于c# - wgs​​ 点与 wgs 定义线段的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10045544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com