多边形的对角线是在里面还是在外面?

发布于 2024-09-01 04:45:01 字数 636 浏览 2 评论 0原文

我有三个连续的多边形点,例如 p1,p2,p3。现在我想知道p1和p3之间的正交是在多边形内部还是在多边形外部。

我通过取三个向量 v1、v2 和 v3 来做到这一点。多边形中点 p1 之前的点称为 p0。
<代码> v1 = (p0 - p1)
v2 = (p2 - p1)
v3 = (p3 - p1)

参考这个问题,我正在使用该问题的已接受答案中显示的方法。它仅适用于逆时针方向。如果我的点是顺时针方向怎么办?

我也知道我的整个多边形是顺时针或逆时针的。因此我选择向量 v1 和 v2。但我仍然遇到一些问题。我正在展示一个我遇到问题的案例。

替代文本

这个多边形是逆时针方向的。是从v1和v2的原点开始的。

I have three consecutive points of polygon, say p1,p2,p3. Now I wanted to know whether the orthogonal between p1 and p3 is inside the polygon or outside the polygon.

I am doing it by taking three vectors v1,v2 and v3. And the point before the point p1 in polygon say p0.

v1 = (p0 - p1)
v2 = (p2 - p1)
v3 = (p3 - p1)

With reference to this question, I am using the method shown in the accepted answer of that question. It is only for counterclockwise. What if my points are clockwise.

I am also knowing my whole polygon is clockwise or counterclockwise. And accordingly I select the vectors v1 and v2. But still I am getting some problem. I am showing one case where I am getting problem.

alt text

This polygon is counterclockwise. and It is starting from the origin of v1 and v2.

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

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

发布评论

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

评论(3

不知所踪 2024-09-08 04:45:01

由于你的点是连续的,你可以通过检查三角形 p1 p2 p3 的方向来解决这个问题。如果方向与多边形的方向相同,则对角线在内部,否则在外部。

要确定三角形的方向,最简单的方法是计算有符号面积并检查符号。计算

p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p2.x * p1.y - p3.x * p2.y - p1.x * p3.y

如果该值的符号为正,则方向为逆时针。如果符号为负,则方向为顺时针方向。

准确地说,上述方法仅提供对角线位于多边形哪一侧的信息。显然,多边形仍然可以在稍后的点与对角线相交。

Since your points are cnosecutive, you can solve this problem by checking the orientation of the triangle p1 p2 p3. If the orientation is the same as the one of the polygon, then the diagonal is in the inside, else on the outside.

To determine the orientation of the triangle, the simplest way is to compute the signed area and check the sign. Compute

p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p2.x * p1.y - p3.x * p2.y - p1.x * p3.y

If the sign of this value is positive, the orientation is counterclockwise. If the sign is negative, the orientation is clockwise.

To be precise, the above method only gives you information on which side of the polygon the diagonal lies. Obviously, the polygon can still intersect the diagonal at later points.

贵在坚持 2024-09-08 04:45:01

基本上,对角线可以完全在内部、完全在外部、既在内部又在外部,并且在所有三种情况下都可能与一条或多条边重叠。这使得确定您的需求变得非常简单。

从数学的角度来看,除了外部面积无限大这样的小细节之外,内部和外部实际上没有太大区别。 (至少对于 2D 平面;在球体上,多边形的内部和外部没有明显区分。)

您还有一个关于多边形边的顺序的子问题。最简单的方法是按顺序将相邻边之间的所有角度相加。这总计为 N*(pi/2)。对于 CCW 多边形,N 为正。

[编辑]
一旦你知道了方向,并且如果你没有上面列出的困难情况,那么问题就很简单了。角度p0-p1-p2小于角度p0-p1-p3。因此,边p1-p3至少部分位于多边形外部。如果它没有穿过其他边,那么它显然完全位于多边形之外。

Basically, a diagonal can be fully inside, fully outside, both inside and outside, and possibly overlapping one or more edges in all three cases. This makes it not entirely trivial to determine what you need.

From a mathematical side, there is actually not that much difference between the inside and the outside, except for such small details as the outside having infinite area. (At least for a 2D plane; on a sphere the inside and outside of a plygon are not sharply distinguished.)

You also have a subquestion about the ordering of your polygon edges. The easiest way is to sum all angles between adjacent edges in order. This will add up to N*(pi/2). For CCW polygons, N is positive.

[edit]
Once you know the direction, and if you have none of the hard cases listed above, the question is easy. The angle p0-p1-p2 is smaller than the angle p0-p1-p3. Hence, the edge p1-p3 lies at least partially outside the polygon. And if it crosses no other edge, it obviously lies fully outside the polygon.

喜爱纠缠 2024-09-08 04:45:01

任意两个向量之间的角度是

alpha = acos(v1.x * v2.x + v1.y * v2.y)

因为你现在可以得到之间的角度

v1 and v3 = alpha1; v1 and v2 = alpha2; 

你可以检查 alpha2 是否在 alpha1 之内:

function normalize(a):
    if a > 2 * pi then a - 2 * pi
    else if a < 2 * pi then a + 2 * pi
    else a

alpha1 = normalize(alpha1)
alpha2 = normalize(alpha2)

if (alpha2 < alpha1) then is_between
else is_not_between

这不是很完整,但你应该明白了。

编辑:如果多边形重叠,正如 MSalters 指出的那样,它将不起作用。

Angle between any two vectors is

alpha = acos(v1.x * v2.x + v1.y * v2.y)

Since you now can have the angle between

v1 and v3 = alpha1; v1 and v2 = alpha2; 

You can check if alpha2 is inside alpha1:

function normalize(a):
    if a > 2 * pi then a - 2 * pi
    else if a < 2 * pi then a + 2 * pi
    else a

alpha1 = normalize(alpha1)
alpha2 = normalize(alpha2)

if (alpha2 < alpha1) then is_between
else is_not_between

This is not very complete, but you should get the idea.

EDIT: it won't work if the polygon is overlapping as MSalters noted.

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