从点获取曲线细节
我有一个 2D 点的 List
。迭代点以确定点列表是直线还是弯曲(以及弯曲程度)的有效方法是什么。我想避免简单地获得较小子集之间的斜率。我该怎么做呢?
感谢您的帮助
编辑:感谢您的回复。澄清一下,我不需要它在数字上准确,但我想确定用户是否使用鼠标创建了弯曲形状,如果是,则曲线有多尖锐。这些值并不是太重要,只要可以确定尖锐曲线和稍微柔和的曲线之间的差异即可。
I have a List
of 2D points. What's an efficient way of iterating through the points in order to determine whether the list of points are in a straight line, or curved (and to what degree). I'd like to avoid simply getting slopes between smaller subsets. How would I go about doing this?
Thanks for any help
Edit: Thanks for the response. To clarify, I don't need it to be numerically accurate, but I'd like to determine if the user has created a curved shape with their mouse and, if so, how sharp the curve is. The values are not too important, as long as it's possible to determine the difference between a sharp curve and a slightly softer one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只是想知道是否所有点或多或少都符合
d
度的曲线,只需在端点上应用拉格朗日插值,然后数组内部的d-2
等距点。这将为您提供一个d
次数的多项式。获得曲线后,只需迭代数组并查看每个点距曲线有多远。如果它们超出阈值,则您的数据不符合您的
d
次多项式。编辑:我应该提到的是,迭代 d 的值是一个有限的过程。一旦
d
达到您拥有的点数,由于拉格朗日插值的工作原理,您将获得完美的拟合。If you simply want to know if all your points fit more or less on a curve of degree
d
, simply apply Lagrange interpolation on the endpoints andd-2
equally spaced points from inside your array. This will give you a polynomial of degreed
.Once you have your curve, simply iterate over the array and see how far away from the curve each point is. If they're farther than a threshold, your data doesn't fit your degree
d
polynomial.Edit: I should mention that iterating through values of
d
is a finite process. Onced
reaches the number of points you have, you'll get a perfect fit because of how Lagrange interpolation works.要测试它是否是直线,请计算相关系数。我确信维基百科上对此有所介绍。
测试它是否弯曲更复杂。您需要知道您期望什么样的曲线,并根据这些曲线进行拟合。
To test if it's a straight line, compute the correlation coefficient. I'm sure that's covered on wikipedia.
To test if it's curved is more involved. You need to know what kind of curves you expect, and fit against those.
下面是计算角度的方法:使用 C# 计算两点之间的角度
简单计算两点之间的角度列表中的每个点并创建角度列表,然后比较角度列表值是否不同。如果它们没有不同,则意味着它是直线,否则它是曲线...
如果它是直线,则所有点之间的角度必须相同。
Here is a method to calculate angle: Calculate Angle between 2 points using C#
Simply calculate angle between each and every point in your list and create list of angles, then compare if angles list values are different. If they are not different then it means it's straight line, otherwise it's curve...
If it's a straight line then angle between all points has to be a same.
这里的问题真的很模糊:“我想避免简单地在较小的子集之间获得斜率”
您可能需要插值a-la B样条线。如果我没记错的话,他们使用了两个点和两个额外的控制点。自很久以前(至少 1980 年代)以来,实现就无处不在。这应该可以帮助您开始。
请记住,您可能需要添加控制点以使曲线与端点相交。确保达到这些目标的一个技巧是简单地将端点复制为额外的控制点。
干杯
更新添加了codeproject的链接
看来我记忆中的 80 年代可能是贝塞尔曲线——某种意义上的前身。
The question is really hazy here: "I'd like to avoid simply getting slopes between smaller substes"
You probably want interpolation a-la B-splines. They use two points and two extra control points if memory serves me. Implementations are ubiquitous since way back (at least 1980's). This should get you underway
Remember that you'll probably need to add control points to make the curve meet the endpoints. One trick to make sure those are reached is to simply duplicate the endpoints as extra controlpoints.
Cheers
Update Added link to codeproject
it would appear that what I remember from back in the 80's could have been Bezier curves - a predecessor of sorts.