如何找到矢量路径的方向
我有一个矢量路径(由 lineto、curveTo 点组成),我想确定它的方向(顺时针、逆时针)。
有没有可以用于相同目的的算法?
我想这样做的原因是我从不同的函数获得两条路径;一个始终是顺时针方向,但另一个函数有时返回逆时针路径;当我组合这两条路径并填充生成的路径时,非零缠绕填充规则会在路径重叠的区域中创建一个孔。
所以我试图通过在组合之前将所有路径转换为顺时针方向来解决问题。
I have a vector path (made up from lineto, curveTo points) and I want to determine its direction (clockwise, anticlockwise).
Is there any algorithm which can be used for the same?
The reason I want to do this is that I am getting two paths from different functions; one is always clockwise but the other function returns anticlockwise paths sometimes; when I combine these two paths and fill the resultant path then the non-zero winding fill rule creates a hole in the regions where the path overlaps.
So I am trying to solve the problem by converting all paths to clockwise direction before combining them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:请参阅下面的评论:
什么是顺时针?举个例子。你认为这条路径是顺时针的吗?:
如果你说是,那这个呢一个?
遵循我最初的建议,该建议适用于凸路径:
您可以使用 3D 向量的叉积。
如果 a,b 两个向量满足:
那么叉积为
现在,如果您假设您描述的路径不与自身相交(这使顺时针或逆时针的点无效),那么您所需要的就是前三点按其出现的顺序排列。从它们中你可以创建 2 个向量,你可以计算它们的叉积。
因此,假设您按顺序掌握了以下几点:
您创建以下向量:
然后您所需要的只是 AxB 的第三个坐标,即:
如果此坐标为正值则你的路径是逆时针方向,如果是负值则顺时针方向。
EDIT: see the comments below:
What is clockwise? Case in point. Do you consider this path clockwise?:
If you said yes, then what about this one?
Follows my original proposal which works fine for convex paths:
You can use the cross product of 3D vectors.
If a,b two vectors such that :
Then the cross product is
Now if you assume that the path you describe does not intersect itself (which nullifies the point of clockwise or counterclockwise) then all you need is the first three points of it in the order they are presented. And from them you create 2 vectors whose cross product you can calculate.
So assuming you have these points in order:
you create the following vectors:
and then all you need is the third coordinate of AxB which is:
If this coordinate is positive then your path is counterclockwise, if it is negative then it is clockwise.
选取路径组件内部的任意点,然后使用填充规则确定其缠绕数是 +1 还是 -1:
(如果路径分量很简单,则只会有一次交叉。如果路径不简单,即它与自身相交,则它如果它是顺时针或不是顺时针,则没有任何意义。)
然后,由于您想要顺时针分量,因此您应该期望计数为+1。计数为 -1 表示该组件由内向外。
Pick any point in the interior of the path component, then use the fill rule to determine whether its winding number is +1 or −1:
(If the path component is simple there will only be one crossing. If the path is not simple, i.e. it crosses itself, then it does not mean anything to say if it is clockwise or not.)
Then, since you want clockwise components you should expect a count of +1. A count of −1 indicates that the component is inside out.