Equidistant points in a line segment

后端 未结 3 915
伪装坚强ぢ
伪装坚强ぢ 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:53

    I'm not sure if I understand your question, but are you trying to divide a line segment like this?

    Before:

    A +--------------------+ B

    After:

    A +--|--|--|--|--|--|--+ B

    Where "two dashes" is your minimum distance? If so, then there'll be infinitely many sets of points that satisfy that, unless your minimum distance can exactly divide the length of the segment. However, one such set can be obtained as follows:

    1. Find the vectorial parametric equation of the line
    2. Find the total number of points (floor(length / minDistance) + 1)
    3. Loop i from 0 to n, finding each point along the line (if your parametric equation takes a t from 0 to 1, t = ((float)i)/n)

    [EDIT] After seeing jerryjvl's reply, I think that the code you want is something like this: (doing this in Java-ish)

    List FindAllPointsInLine(Point start, Point end, float distance)
    {
        float length = Math.hypot(start.x - end.x, start.y - end.y);
        int n = (int)Math.floor(length / distance);
        List result = new ArrayList(n);
    
        for (int i=0; i<=n; i++) {  // Note that I use <=, not <
            float t = ((float)i)/n;
            result.add(interpolate(start, end, t));
        }
    
        return result;
    }
    
    Point interpolate(Point a, Point b, float t)
    {
        float u = 1-t;
        float x = a.x*u + b.x*t;
        float y = a.y*u + b.y*t;
        return new Point(x,y);
    }
    

    [Warning: code has not been tested]

提交回复
热议问题