简体   繁体   English

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

[英]How to compute the intersection points of a line and an arbitrary shape?

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.我知道路径中所有线条和曲线的位置,如果有帮助,我正在使用 HTML5 画布元素。 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.例如,如果 API 是用 lineTo、moveTo 和 arc 调用的,我就拥有所有这些信息。 Each call to the API is stored in an array.对 API 的每次调用都存储在一个数组中。 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.下面的图像显示了我需要找到的点的示例。

替代文字

Thanks for any help!谢谢你的帮助! Again, I would rather do this geometrically rather than pixel based if possible.同样,如果可能的话,我宁愿以几何方式而不是基于像素来进行此操作。

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

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.本书中有关于直线段算法的解决方案。

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. 这个讨论对相交线和贝塞尔曲线有一些很好的建议。

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).您想将一条线和一条“样条”x(t), y(t) 相交,对于 x(t) 和 y(t),它最多应该是 4 次多项式。 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 样条曲线和直线之间的交点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM