线段中的等距点 [英] Equidistant points in a line segment

查看:314
本文介绍了线段中的等距点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 equidistant from each point closest to it with a minimal distant apart.

I

List<'points> FindAllPointsInLine(Point start, Point end, int minDistantApart)  
{  
//    find all points  
}


推荐答案

将问题解释为:


  • 在点之间开始

  • 并指向 end

  • 间隔至少等于 minDistanceApart

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

然后,这非常简单:开始结束之间的长度除以 minDistanceApart ,四舍五入为负1。(没有负1时,您得到的是端点之间的距离数而不是端点之间的加分数)。

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)

实现:

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;
}

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

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.

这篇关于线段中的等距点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆