点之间的均匀距离
如果路径是由多个彼此距离不均匀的点定义的,那么我怎么能沿着同一条路径重新定义相同数量但距离均匀的点呢?我正在尝试在 Objective-C 中使用 CGPoint
的 NSArray
来完成此操作,但到目前为止我还没有遇到任何运气。 感谢您的帮助。
编辑
我想知道这是否有助于减少点数,例如当检测 3 个点是否共线时,我们可以删除中间的一个,但我不确定这会有所帮助。
编辑
插图: 红色是原始点,蓝色是后处理点:
蓝点定义的新路径与原始路径不对应。
How could I, having a path defined by several points that are not in a uniform distance from each other, redefine along the same path the same number of points but with a uniform distance. I'm trying to do this in Objective-C with NSArray
s of CGPoint
s but so far I haven't had any luck with this.
Thank you for any help.
EDIT
I was wondering if it would help to reduce the number of points, like when detecting if 3 points are collinear we could remove the middle one, but I'm not sure that would help.
EDIT
Illustrating:
Reds are the original points, blues the post processed points:
The new path defined by the blue dots does not correspond to the original one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你不能做你所说的你想做的事。但这可能是我的误解。例如,我从您的评论中了解到,连续点之间的路径是笔直的,而不是弯曲的。
例如,一条由 3 个点 (0,1,2) 和 2 条不同长度的线段 (0-1,1-2) 组成的简单路径。将点 0 和 2 保留在原处,并引入一个新点 1',该点与点 0 和 2 等距。如果点 1' 位于线段 0-1、1-2 之一上,则线段 0 之一上-1', 1'-2 与 0-1, 1-2 不一致。 (更容易绘制它,我建议您这样做。)如果点 1' 不在任何一条原始线段上,则除了端点之外,整个路径都是新的。
那么,你想要新路径和旧路径之间有什么关系呢?
编辑:实际上更多的是扩展评论,就像我的“答案”,但评论框太小了。
我仍然不清楚你想如何定义新路径以及它与旧路径有什么关系。首先,您想保留相同数量的点,但在您的编辑中您说这是没有必要的。您同意用新点替换点会改变路径。您是否想要一条从点 0 到点 N-1 的新路径,该路径由路径上均匀间隔的 N 个点定义,以便在笛卡尔平面上绘制时最小化新旧路径之间的面积?
或者,也许您可以首先定义通过原始点的多项式(或样条线或其他简单曲线)路径,然后沿着曲线来回移动点,直到它们均匀间隔?
I don't think you can do what you state that you want to do. But that could be a misunderstanding on my part. For example, I have understood from your comment that the path is straight between successive points, not curved.
Take, for example, a simple path of 3 points (0,1,2) and 2 line segments (0-1,1-2) of different lengths. Leave points 0 and 2 where they are and introduce a new point 1' which is equidistant from points 0 and 2. If point 1' is on one of the line segments 0-1, 1-2, then one of the line segments 0-1', 1'-2 is not coincident with 0-1, 1-2. (Easier to draw this, which I suggest you do.) If point 1' is not on either of the original line segments then the entire path is new, apart from its endpoints.
So, what relationship between the new path and the old path do you want ?
EDIT: more of an extended comment really, like my 'answer' but the comment box is too small.
I'm still not clear how you want to define the new path and what relationship it has to the old path. First you wanted to keep the same number of points, but in your edit you say that this is not necessary. You agree that replacing points by new points will shift the path. Do you want, perhaps, a new path from point 0 to point N-1, defined by N points uniformly spaced on a path which minimises the area between the old and new paths when drawn on the Cartesian plane ?
Or, perhaps you could first define a polynomial (or spline or other simple curve) path through the original points, then move the points to and fro along the curve until they are uniformly spaced ?
我认为这个问题很简单,实际上很容易解决:)
基本思想是:
首先检查当前点(P)和所在线段终点之间的距离是否 >= 之间的距离P 和下一点 (Q)。
如果是,那就太好了,我们使用一些简单的三角函数来计算它。
否则,我们将移至相邻线段(按您的顺序)并扣除 P 与您所在线段端点之间的距离并继续该过程。
伪代码:
之前定义的
功能:沿着路径查找下一个点
入口点
I think the problem is simple and easily solvable actually :)
The basic idea is:
First check if the distance between your current point (P) and the end point of the line segment you are on is >= the distance between P and the next point (Q).
If it is, great, we use some simple trigonometry to figure it out.
Else, we move to the adjacent line segment (in your ordering) and deduct the distance between P and the endpoint of the line segment you are on and continue the process.
Pseudocode:
Defined previously
Function: Find the next point along your path
Entry point
我的感觉是这是一个非常困难的问题。
它基本上相当于一个约束优化问题。目标函数衡量新线与旧线的接近程度。约束强制新点之间的距离相同。
找到一个好的目标函数是棘手的一点,因为它必须是可微分的,而且我们提前不知道每个新点将位于哪些段上:例如,两个新点可能位于超长的线上旧段,并且在某些超短旧段上没有新点。如果您以某种方式先验地知道新点将位于哪些线段上,则可以将点与其目标线段之间的距离相加,并将其用作目标函数(请注意,此距离函数是不平凡的,因为线段是有限的:它是由三部分组成,其水平集是“药丸形”。)
或者您可能会忘记要求新点位于旧线段上,而只是寻找一条“接近”旧点的新折线。例如,您可能会尝试在折线之间写下类似 L2 的度量,并将其用作目标函数。我不认为这个指标写起来或区分起来会很愉快。
My sense is that this is a very hard problem.
It basically amounts to a constrained optimization problem. The objective function measures how close the new line is from the old one. The constraints enforce that the new points are the same distance apart.
Finding a good objective function is the tricky bit, since it must be differentiable, and we don't know ahead of time on which segments each new point will lie: for instance, it's possible for two new points to lie on an extra-long old segment, and no new points lying on some extra-short old segment. If you somehow know a priori on which segments the new points will lie, you can sum the distances between points and their target segments and use that as your objective function (note that this distance function is nontrivial, since the segments are finite: it is composed of three pieces and its level-sets are "pill-shaped.")
Or you might forget about requiring the new points to lie on old segments, and just look for a new polyline that's "close" to the old one. For instance, you might try to write down an L2-like metric between polylines, and use that as your objective function. I don't expect this metric to be pleasant to write down, or differentiate.
我认为扰动方法适用于此。
我假设:
只需迭代剩余的 (n-2) 个点:如果点 k 距离点 (k-1) 比点 (k+1) 更近,则将其沿路径向前移动一点。同样,如果它更接近点 (k+1),则沿着路径向后移动一点。
最好从较大的步长开始(为了速度),然后减小步长(为了精度)。即使这些点相互交叉,我认为这种方法也会将它们重新排序。
I think a perturbative approach will work for this one.
I assume:
just iterate over the remaining (n-2) points: if point k is closer to point (k-1) than to point (k+1), move it a little forward along the path. Likewise if it's closer to point (k+1), move a little back along the path.
It's probably best to start with large step sizes (for speed) then make them smaller (for precision). Even if the points pass each other, I think this approach will sort them back into order.
这将使用相当多的向量数学,但实际上非常简单。
首先,您需要找到路径的总距离。您将如何执行取决于路径点的存储方式。这是伪代码中的二维路径的基本示例。
最后一步是让它不合理的原因,但这应该对你有用。
某处可能存在一个小的数学错误,我多次检查了这一点,但可能有一些我错过的东西。因此,如果有人注意到某些内容,请通知我,我将对其进行编辑。
希望这有帮助,
大风
This will use quite a bit of vector math but is quite simple really.
First you will need to find the total distance of the path. Depending on how the points of the path are stored is how you will do it. Here is a basic example on a 2 Dimensional Path in Pseudo-code.
The last step is what makes it unreasonable but this should work for you.
There may be a small math error somewhere I double checked this several times but there could be something I missed. So if anyone notices something please inform me and I will edit it.
Hope this helps,
Gale