Equidistant points in a line segment

后端 未结 3 916
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 06:13

Let assume you have two points (a , b) in a two dimensional plane. Given the two points, what is the best way to find the maximum points on the line segment that are equidistan

3条回答
  •  无人共我
    2021-01-25 06:50

    Interpreting the question as:

    • Between point start
    • And point end
    • What is the maximum number of points inbetween spaced evenly that are at least minDistanceApart

    Then, that is fairly simply: the length between start and end divided by minDistanceApart, rounded down minus 1. (without the minus 1 you end up with the number of distances between the end points rather than the number of extra points inbetween)

    Implementation:

    List 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 result = new List;
    
        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;
    }
    

    If you want all the points, including the start and end point, then you'll have to adjust the for loop, and start 'px' and 'py' at 'start.x' and 'start.y' instead. Note that if accuracy of the end-points is vital you may want to perform a calculation of 'px' and 'py' directly based on the ratio 'ix / numPoints' instead.

提交回复
热议问题