假设您在二维平面中有两个点(a,b)。鉴于这两点,找到线段上与距离它最近的每个点等距的最佳点的最佳方法是什么。
我使用C#,但任何语言的示例都会有所帮助。
List<'points> FindAllPointsInLine(Point start, Point end, int minDistantApart)
{
// find all points
}
答案 0 :(得分:4)
将问题解释为:
start
end
minDistanceApart
然后,这很简单: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;
}
如果你想要所有的点,包括起点和终点,你就必须调整for循环,然后在'start.x'和'start.y'处开始'px'和'py' 。请注意,如果终点的准确性至关重要,您可能希望直接根据比率'ix / numPoints'执行'px'和'py'的计算。
答案 1 :(得分:2)
我不确定我是否理解你的问题,但你是否试图划分这样的线段?
在:
A + -------------------- + B
后:
A + - | - | - | - | - | - | - + B
“两个破折号”是你的最小距离?如果是这样,那么将有无限多组满足该点的点,除非您的最小距离可以精确地划分段的长度。但是,可以如下获得一个这样的集合:
[编辑] 在看到jerryjvl的回复之后,我认为你想要的代码是这样的:(在Java-ish中这样做)
List<Point> 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<Point> result = new ArrayList<Point>(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);
}
[警告:代码尚未经过测试]
答案 2 :(得分:1)
找出适合该行的点数。计算X和Y坐标的步数并生成点。像这样:
lineLength = sqrt(pow(end.X - start.X,2) + pow(end.Y - start.Y, 2))
numberOfPoints = floor(lineLength/minDistantApart)
stepX = (end.X - start.X)/numberOfPoints
stepY = (end.Y - start.Y)/numberOfPoints
for (i = 1; i < numberOfPoints; i++) {
yield Point(start.X + stepX*i, start.Y + stepY*i)
}