如何找到矢量路径的方向

发布于 2024-10-07 11:42:54 字数 222 浏览 3 评论 0原文

我有一个矢量路径(由 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 技术交流群。

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

发布评论

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

评论(2

‖放下 2024-10-14 11:42:54

编辑:请参阅下面的评论:

什么是顺时针?举个例子。你认为这条路径是顺时针的吗?:

alt text

如果你说是,那这个呢一个?

alt text

遵循我最初的建议,该建议适用于凸路径:

您可以使用 3D 向量的叉积。

如果 a,b 两个向量满足:

a = (a1, a2, a3)

b = (b1, b2, b3)

那么叉积为

axb = (a2b3-a3b2, a3b1-a1b3, a1b2 - a2b1)

现在,如果您假设您描述的路径不与自身相交(这使顺时针或逆时针的点无效),那么您所需要的就是前三点按其出现的顺序排列。从它们中你可以创建 2 个向量,你可以计算它们的叉积。

因此,假设您按顺序掌握了以下几点:

a = (a1, a2)

b = (b1, b2)

c = (c1, c2)

您创建以下向量:

A = (A1, A2) = ab = (b1-a1, b2-a2)

B = (B1, B2) = bc = (c1-b1, c2-b2)

然后您所需要的只是 AxB 的第三个坐标,即:

A1B2 - A2B1

(b1-a1)(c2-b2) - (b2-a2)(c1-b1)

如果此坐标为正值则你的路径是逆时针方向,如果是负值则顺时针方向。

EDIT: see the comments below:

What is clockwise? Case in point. Do you consider this path clockwise?:

alt text

If you said yes, then what about this one?

alt text

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 :

a = (a1, a2, a3)

b = (b1, b2, b3)

Then the cross product is

axb = (a2b3-a3b2, a3b1-a1b3, a1b2 - a2b1)

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:

a = (a1, a2)

b = (b1, b2)

c = (c1, c2)

you create the following vectors:

A = (A1, A2) = ab = (b1-a1, b2-a2)

B = (B1, B2) = bc = (c1-b1, c2-b2)

and then all you need is the third coordinate of AxB which is:

A1B2 - A2B1

or

(b1-a1)(c2-b2) - (b2-a2)(c1-b1)

If this coordinate is positive then your path is counterclockwise, if it is negative then it is clockwise.

九八野马 2024-10-14 11:42:54

选取路径组件内部的任意点,然后使用填充规则确定其缠绕数是 +1 还是 -1:

  1. 从该点到无穷远绘制一条射线
  2. 从计数为零开始
  3. 每次路径添加 1线段从左到右穿过射线 路径段
  4. 每次从右到左穿过射线时减 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:

  1. Draw a ray from the point out to infinity
  2. Start with a count of zero
  3. Add 1 every time a path segment crosses the ray from left to right
  4. Subtract 1 every time a path segment crosses the ray from right to left

(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.

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