将问题解释为:
startendminDistanceApart然后,这很简单:start和end除以之间的长度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' 的计算。