查找一个点是否位于多个矩形之一中的最快方法是什么?

发布于 2025-01-16 13:05:21 字数 221 浏览 3 评论 0原文

所以基本上我是为我的 Minecraft spigot 插件(java)这样做的。我知道已经有一些土地所有权插件,但我想制作自己的。 对于这个声明插件,我想知道如何获取一个点(minecraft 块)是否在一个区域(矩形)内。我知道如何检查一个点是否在矩形内,主要问题是当有 10.000 个矩形时如何尽快检查。

检查 10.000 甚至 100.000 的最有效方法是什么,而无需手动循环所有这些并检查每个矩形?

So basically im doing this for my minecraft spigot plugin (java). I know there are already some land claim plugins but i would like to make my own.
For this claim plugin i'd like to know how to get if a point (minecraft block) is inside a region (rectangle). i know how to check if a point is inside a rectangle, the main problem is how to check as quickly as possible when there are like lets say 10.000 rectangles.

What would be the most efficient way to check 10.000 or even 100.000 without having to manually loop through all of them and check every single rectangle?

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

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

发布评论

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

评论(2

东北女汉子 2025-01-23 13:05:22

如果您的矩形在一个大矩形中均匀地彼此相邻,那么找到哪个矩形包含点很容易:

width = (maxX-minX)/num_rectangles_x;
height = same but for y
idx = floor( (x - minX)/width );
idy = floor( (y - minY)/height );
id_square = idx + idy*num_rectangles_x;

如果您的矩形是随机放置的,那么您应该使用像八叉树这样的空间加速结构。然后检查点是否在根中,然后检查点是否在其节点之一中,重复直到找到包含该点的叶子。 CPU 上应该可以达到每 10 毫秒 10000 次测试。对于 GPU 来说,每 10 毫秒 100 万次测试应该没问题。但是您可能需要实现八叉树的稀疏版本和叶节点的空间填充曲线顺序,以获得更好的缓存,以达到这些性能水平。

If your rectangles are uniformly placed neighbors of each other in a big rectangle, then finding which rectangle contains point is easy:

width = (maxX-minX)/num_rectangles_x;
height = same but for y
idx = floor( (x - minX)/width );
idy = floor( (y - minY)/height );
id_square = idx + idy*num_rectangles_x;

If your rectangles are randomly placed, then you should use a spatial acceleration structure like octree. Then check if point is in root, then check if point is in one of its nodes, repeat until you find a leaf that includes the point. 10000 tests per 10milliseconds should be reachable on cpu. 1 million tests per 10ms should be ok for a gpu. But you may need to implement a sparse version of the octree and a space filling curve order for leaf nodes to have better caching, to reach those performance levels.

浪荡不羁 2025-01-23 13:05:21

当矩形以检查它们是否保留该点的方式生成时,有没有办法添加逻辑测试?在这种情况下,如果它们在生成时包含该点,则可以将布尔值设置为 true,然后在检查该 Minecraft 块时,区域(矩形)回复 true 或 false。

这样,您可以在生成矩形时运行循环或检查,但在运行游戏时,回复应该会非常快地发生,只需检查 bool ContainsPoint 的 true 或 false 即可。

Is there a way to add a logical test when the rectangles get generated in a way that checks if they hold that point? In that case you could set a boolean to true if they contain that point when generated, and then when checking for that minecraft block the region (rectangle) replies with true or false.

This way you run the loops or checks when generating the rectangles, but when running the game the replies should happen very fast, just check if true or false for bool ContainsPoint.

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