【问题标题】:Find out all the points on a line using midpoint formula使用中点公式找出一条线上的所有点
【发布时间】:2014-01-14 11:51:05
【问题描述】:

我在地图上绘制了一条路线(线)。对于这条路线,我有 2 个点,起点和终点分别是 (x1,y1) 和 (x2, y2)。

我需要找出线上的所有点。我为此使用中点公式。 以下是相同的代码。

private void GetAllPoints(GraphLocation start, GraphLocation end)
{
    GraphLocation midPoint = GetMidPoint(start, end);
    allPoints.Add(midPoint);
    while (start.Latitude != midPoint.Latitude && start.Longitude != midPoint.Longitude)
    {
        GraphLocation point = GetMidPoint(start, midPoint);
        midPoint = point;
        allPoints.Add(point);
    }          
}

private GraphLocation GetMidPoint(GraphLocation start, GraphLocation end)
{
    GraphLocation midPoint = new GraphLocation();

    double dLon = DegreesToRadians(end.Longitude - start.Longitude);
    double Bx = Math.Cos(DegreesToRadians(end.Latitude)) * Math.Cos(dLon);
    double By = Math.Cos(DegreesToRadians(end.Latitude)) * Math.Sin(dLon);

    midPoint.Latitude = RadiansToDegrees(Math.Atan2(
                         Math.Sin(DegreesToRadians(start.Latitude)) + Math.Sin(DegreesToRadians(end.Latitude)),
                         Math.Sqrt(
                             (Math.Cos(DegreesToRadians(start.Latitude)) + Bx) *
                             (Math.Cos(DegreesToRadians(start.Latitude)) + Bx) + By * By)));

   midPoint.Longitude = start.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(start.Latitude)) + Bx));
   allPoints.Add(midPoint);

   return midPoint;
}

此代码跳过了路线的某些部分。

请帮助我获得想要的结果。

【问题讨论】:

  • 对找出一行上所有点的方程式或代码有问题吗?
  • 一条线有无数个点...究竟想要的结果是什么?
  • 我尝试了一种方法,递归地使用中点公式断线,除非所有点都被覆盖。我无法弄清楚停止条件。
  • 没有停止条件。一条线有无数个点。再说一次,你想做什么?为什么需要生成这些点?这是什么地图?这是什么路线?你的目标是什么?您如何确定您“缺少”积分,或者某些积分被“跳过”?
  • 你是否试图沿着由这两个端点定义的线段生成一个均匀分布的点数组?

标签: c# algorithm latitude-longitude points


【解决方案1】:

您似乎只在该行的前半部分计算中点,然后每次都将其二等分,因为您只在开始和中点之间进行计算。 GraphLocation point = GetMidPoint(start, midPoint); 递归调用 GetMidPoint() 怎么样?停止条件是限制递归的深度,所以你不会得到很多点。

【讨论】:

  • 是的,你是对的,这就是我的问题。我无法弄清楚递归的停止条件。
  • 为什么不给出一个整数,例如。 2000 到第一次调用 GetMidPoint()。每次递归更深时,您减去 1 直到达到 0 ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 2013-05-11
  • 1970-01-01
  • 2022-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多