如何计算直线和任意形状的交点?

发布于 2024-09-06 11:21:33 字数 398 浏览 5 评论 0原文

有没有一种方法可以几何计算一条线和任意图形路径的交点?我知道所有直线和曲线在路径中的位置,并且我正在使用 HTML5 canvas 元素(如果有帮助的话)。基本上,我可以访问所有画布绘图命令及其参数。例如,如果使用 lineTo、moveTo、arc 调用 API,我就拥有所有这些信息。对 API 的每次调用都存储在一个数组中。我有路径定义,我只想找出线与路径相交的位置。下面的图片显示了我需要找到的点的示例。

替代文本

感谢您的帮助!再说一次,如果可能的话,我宁愿以几何方式而不是基于像素来完成此操作。

Is there a way to geometrically compute the intersection points of a line and an arbitrary graphics path? I know where all of the lines and curves are in the path, and I am using the HTML5 canvas element if that helps any. Basically, I have access to all of the canvas drawing commands and their arguments. For instance, if the API was called with a lineTo, then a moveTo, then an arc I have all of that information. Each call to the API is stored in an array. I have the path definition, I just want to figure out where the line intersects the path. Below is an image showing an example of the points I would need to find.

alt text

Thanks for any help! Again, I would rather do this geometrically rather than pixel based if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜嗑 2024-09-13 11:21:33

您可能想看看 Kevin Lindsey 的 Javascript 几何库 - 它可能包含您正在寻找的所有相交算法: http://www.kevlindev.com/geometry/index.htm

You might want to have a look at Kevin Lindsey's Javascript geometry library - it contains probably all the interesection algorithms you are looking for: http://www.kevlindev.com/geometry/index.htm

勿忘心安 2024-09-13 11:21:33

如果不知道你的图形路径是如何定义的,就不可能用具体的算法来回答你的问题。 这本关于直线段算法的书中有一个解决方案。

Without knowing how your graphics path is defined, it's impossible to answer your question with a concrete algorithm. There is a solution in this book on algorithms for straight line segments.

请恋爱 2024-09-13 11:21:33

如果你有一切的方程式,那么你就可以做到(理论上)。实际上,这并不那么容易(尤其是在一般情况下)。 此讨论对相交线和贝塞尔曲线有一些很好的建议。

If you have the equations for everything, then you can do it (in theory). In practice, it's not so easy (especially not in the general case). This discussion has some good advice on intersecting lines and bezier curves.

仙女山的月亮 2024-09-13 11:21:33

您想要将一条线和一条“样条线”x(t), y(t) 相交,对于 x(t) 和 y(t) 来说,它最多应该是 4 次多项式。您已经解出了方程,但您确实需要知道所有参数。如果解超出任一范围(线段和样条线段有起点和终点) - 丢弃它。相关q:

样条线和直线之间的交点

You want to intersect a line and a "spline" x(t), y(t) which should be at most 4-th degree polynomial for both x(t) and y(t). You ed up solving equations, but yo do need to know all of the parameters. If solution is out of either range (line segment and spline segment have start and end) - discard it. Related q:

The intersection point between a spline and a line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文