如何确定一系列点(或多边形)是否在矩形区域内?

发布于 2024-11-08 18:44:06 字数 1026 浏览 5 评论 0原文

我一直在查看有关确定一个点是否位于多边形内的帖子,答案对我来说要么太模糊、太抽象,要么太复杂。因此,我将尝试针对我需要做的事情提出我的问题。

我有一组描述非直线(有时是闭合多边形)的点。我有一个矩形的“视图”区域。我需要尽可能有效地确定是否有任何线段(或多边形边界)穿过视图区域。

我不能简单地测试每个点以查看它是否位于视图区域内。一条线段可能穿过该区域,而实际上没有任何点位于该区域内(即穿过该区域绘制线)。

这是我想要确定的示例(红色表示该函数应该为点集返回 true,蓝色表示它应该返回 false,示例使用直线和矩形,因为我不是艺术家)。

红色表示函数应该返回 true,蓝色表示它应该返回 false

我希望能够考虑的另一个条件(尽管该方法/函数可以是单独的),不仅要确定多边形的边界是否穿过矩形区域,还要确定该区域是否完全被多边形包围。这里的细微差别是,在上面首先描述的情况下,如果我只关心绘制边框,则该方法应该返回 false。但在此处描述的情况下,如果我需要填充多边形区域,那么我需要函数返回 true。我目前不需要担心测试“甜甜圈”形状的多边形(感谢上帝!)。

下面是一个说明细微差别的示例(红色矩形没有穿过屏幕区域的单个顶点或边界段,但仍应将其视为在屏幕上)

: sstatic.net/tl0tf.png" alt="红色矩形没有穿过屏幕区域的单个顶点或边框线段,但它仍应被视为在屏幕上。">

对于“是否有任何线段或多边形边界穿过或位于 屏幕?”我知道我可以想出一个解决方案(尽管可能不是一个有效的解决方案)。尽管比较冗长,但条件对我来说很清楚。但第二个“多边形区域在屏幕上吗?”问题有点难。我希望有人对此有一个好的建议。如果一种解决方案可以轻松地在另一种解决方案之上实施,那么,booya。

一如既往,提前感谢您的任何帮助或建议。

PS 我有一个用于确定线相交的函数,但使用它来比较每个线段与屏幕区域的每一侧似乎有些过分,因为屏幕区域始终是一个简单的 [0, 0, width, height]长方形。难道就没有什么捷径吗?

I have been looking at posts about determining if a point lies within a polygon or not and the answers are either too vague, abstract, or complex for me. So I am going to try to ask my question specific to what I need to do.

I have a set of points that describe a non-straight line (sometimes a closed polygon). I have a rectangular "view" region. I need to determine as efficiently as possible whether any of the line segments (or polygon borders) pass through the view region.

I can't simply test each point to see if it lies within the view region. It is possible for a segment to pass through the region without any point actually inside the region (ie the line is drawn across the region).

Here is an example of what I want to determine (red means the function should return true for the set of points, blue means it should return false, example uses straight lines and rectangles because I am not an artist).

Red indicates the function should return true, blue indicates it should return false

Another condition I want to be able to account for (though the method/function may be a separate one), is to determine not just whether a polygon's border passes through the rectangular region, but whether the region is entirely encompassed by the polygon. The nuance here is that in the situation first described above, if I am only concerned with drawing borders, the method should return false. But in the situation described here, if I need to fill the polygon region then I need the function to return true. I currently do not need to worry about testing "donut" shaped polygons (thank God!).

Here is an example illustrating the nuance (the red rectangle does not have a single vertex or border segment passing through the on-screen region, but it should still be considered on-screen):

The red rectangle does not have a single vertex or border segment passing through the on-screen region, but it should still be considered on-screen.

For the "does any line segment or polygon border pass through or lie on screen?" problem I know I can come up with a solution (albeit perhaps not an efficient one). Even though it is more verbose, the conditions are clear to me. But the second "is polygon region on screen?" problem is a little harder. I'm hoping someone might have a good suggestion for doing this. And if one solution is easily implemented on top of the other, well, booya.

As always, thank you in advance for any help or suggestions.

PS I have a function for determining line intersection, but it seems like overkill to use it to compare each segment to each side of the on-screen region because the on-screen region is ALWAYS a plain [0, 0, width, height] rectangle. Isn't there some kind of short-cut?

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

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

发布评论

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

评论(2

迷爱 2024-11-15 18:44:06

您正在搜索的内容被命名为碰撞检测算法 Google 搜索将引导您找到大量各种语言的实现以及大量的理论

背后有大量的几何理论,从最简单的平分线演算到约束德劳内三角剖分和沃罗诺图(这只是示例)。这取决于对象的形状、维数以及所需的精确度和提供的计算时间之间的比率;-)

好读

What you are searching for is named a Collision Detection Algorithm A Google search will lead you to plenty of implementations in various language as well as a lot of theory

There are plenty of Geometric theory behind, from the simplest bisector calculus to Constrained Delaunay Triangulations and Voronoi Diagrams (that are just examples). It depends on the shape of Object, the number of dimensions and for sure the ratio between exactness needed and computing time afforded ;-)

Good read

格子衫的從容 2024-11-15 18:44:06

PS我有一个函数可以确定
线相交,但看起来像
用它来比较每个都太过分了
分段到屏幕的每一侧
区域,因为屏幕上的区域是
始终是普通的 [0, 0, 宽度, 高度]
长方形。难道没有某种
捷径?

这并不是矫枉过正,而是必要的。我能想到的唯一快捷方式是将值 [0, 0, width, height] 硬编码到该函数中并稍微简化一下。

PS I have a function for determining
line intersection, but it seems like
overkill to use it to compare each
segment to each side of the on-screen
region because the on-screen region is
ALWAYS a plain [0, 0, width, height]
rectangle. Isn't there some kind of
short-cut?

It's not an overkill, its neccessary here. The only kind of shortcut I can think of is to hardcode values [0, 0, width, height] into that function and simplify it a bit.

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