根据百分比计算两个坐标之间的点

Calculate point between two coordinates based on a percentage

我正在寻找一个函数,该函数 returns 两点(我还指定了它们的纬度、经度)之间的一个点(纬度、经度)并且该点基于距离百分比。

所以,我在函数上指定了 Lat1、Lon1、Lat2、Lon2 和 %,它 returns 一个点,例如,从第一个点到第二个点的距离为 20%。

假设坐标是十进制数。你可以使用这个等式。

function midpoint(lat1, long1, lat2, long2, per) {
     return [lat1 + (lat2 - lat1) * per, long1 + (long2 - long1) * per];
}

Return [lat, long] 的新期望坐标,基于百分比(例如 per=0.2 表示 20%)。

这是一个很有帮助的参考(查看底部)

http://www.movable-type.co.uk/scripts/latlong.html

中间点

还可以计算两点之间沿大圆路径的任意分数处的中间点。

公式:

a = sin((1−f)⋅δ) / sin δ
b = sin(f⋅δ) / sin δ
x = a ⋅ cos φ1 ⋅ cos λ1 + b ⋅ cos φ2 ⋅ cos λ2
y = a ⋅ cos φ1 ⋅ sin λ1 + b ⋅ cos φ2 ⋅ sin λ2
z = a ⋅ sin φ1 + b ⋅ sin φ2
φi = atan2(z, √x² + y²)
λi = atan2(y, x)

其中 f 是沿大圆路线的分数(f=0 是点 1,f=1是点2),δ是两点之间的angular距离d/R.

这两个答案可能对特定用户有用,但我想指出一些问题。

lguiel 的解决方案适用于游戏或短距离,但不适用于地球上的几何计算。

spirographer 的答案是正确的,但由于它是非常理论化的,因此编程起来可能太难了。

我将第二个答案翻译成实用的编程语言,以便您可以将其用于自己的项目。

Full code | Run online

// Original calculation from https://www.movable-type.co.uk/scripts/latlong.html
LatLng calculateIntermediatePoint(LatLng point1, LatLng point2, double perc) {
  //const φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
  //const φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
  double lat1 = degreesToRadians(point1.latitude);
  double lng1 = degreesToRadians(point1.longitude);
  double lat2 = degreesToRadians(point2.latitude);
  double lng2 = degreesToRadians(point2.longitude);

  //const Δφ = φ2 - φ1;
  //const Δλ = λ2 - λ1;
  double deltaLat = lat2 - lat1;
  double deltaLng = lng2 - lng1;

  //const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ/2) * Math.sin(Δλ/2);
  //const δ = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  double calcA = sin(deltaLat / 2) * sin(deltaLat / 2) +
      cos(lat1) * cos(lat2) * sin(deltaLng / 2) * sin(deltaLng / 2);
  double calcB = 2 * atan2(sqrt(calcA), sqrt(1 - calcA));

  //const A = Math.sin((1-fraction)*δ) / Math.sin(δ);
  //const B = Math.sin(fraction*δ) / Math.sin(δ);
  double A = sin((1 - perc) * calcB) / sin(calcB);
  double B = sin(perc * calcB) / sin(calcB);

  //const x = A * Math.cos(φ1) * Math.cos(λ1) + B * Math.cos(φ2) * Math.cos(λ2);
  //const y = A * Math.cos(φ1) * Math.sin(λ1) + B * Math.cos(φ2) * Math.sin(λ2);
  //const z = A * Math.sin(φ1) + B * Math.sin(φ2);
  double x = A * cos(lat1) * cos(lng1) + B * cos(lat2) * cos(lng2);
  double y = A * cos(lat1) * sin(lng1) + B * cos(lat2) * sin(lng2);
  double z = A * sin(lat1) + B * sin(lat2);

  //const φ3 = Math.atan2(z, Math.sqrt(x*x + y*y));
  //const λ3 = Math.atan2(y, x);
  double lat3 = atan2(z, sqrt(x * x + y * y));
  double lng3 = atan2(y, x);

  //const lat = φ3.toDegrees();
  //const lon = λ3.toDegrees();
  return LatLng(radiansToDegrees(lat3), radiansToDegrees(lng3));
}