线段中的等距点

use*_*116 1 algorithm geometry distance

假设您在二维平面中有两个点 (a, b)。给定这两个点,找到线段上与最接近它的每个点等距且相距最小的最大点的最佳方法是什么。

我使用 C#,但任何语言的示例都会有所帮助。

List<'points> FindAllPointsInLine(Point start, Point end, int minDistantApart)  
{  
//    find all points  
}
Run Code Online (Sandbox Code Playgroud)

jer*_*jvl 5

将问题解释为:

  • 点间 start
  • 并点 end
  • 中间均匀间隔的点的最大数量是多少 minDistanceApart

然后,这很简单:startend除以之间的长度minDistanceApart,向下舍入减 1。(如果没有减 1,您最终会得到端点之间的距离数,而不是中间的额外点数)

执行:

List<Point> FindAllPoints(Point start, Point end, int minDistance)
{
    double dx = end.x - start.x;
    double dy = end.y - start.y;

    int numPoints =
        Math.Floor(Math.Sqrt(dx * dx + dy * dy) / (double) minDistance) - 1;

    List<Point> result = new List<Point>;

    double stepx = dx / numPoints;
    double stepy = dy / numPoints;
    double px = start.x + stepx;
    double py = start.y + stepy;
    for (int ix = 0; ix < numPoints; ix++)
    {
        result.Add(new Point(px, py));
        px += stepx;
        py += stepy;
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

如果你想要所有的点,包括起点和终点,那么你必须调整 for 循环,并从 'start.x' 和 'start.y' 开始 'px' 和 'py' 。请注意,如果端点的准确性至关重要,您可能希望直接根据比率 'ix / numPoints' 执行 'px' 和 'py' 的计算。