如何快速选择沿给定角度的一组网格单元?

发布于 2025-01-08 01:21:18 字数 877 浏览 0 评论 0原文

我已经设置了一个包含理论单元的网格类。

我有一个方法collectCells(),它接受起点、弧度和距离作为参数。我希望此方法返回一个数组,其中包含沿给定角度且距起点指定距离内的所有单元格,例如

在此处输入图像描述

对我来说,执行此操作的最简单方法就是循环遍历给定方向上的所有像素,并在执行时选取单元格,例如:

for(var i:int = 0; i<distance; i++)
{
    startPoint.x += Math.cos(radians);
    startPoint.y += Math.sin(radians);

    if( start point falls within uncollected cell) collect cell;
}

这显然非常糟糕,因为循环将是只要是我指定的距离——非常慢。

我尝试了一些不同的方法,创建了一个接受最后收集的单元格和弧度的 nextCell() 方法。该方法将一个点沿弧度指定的方向抛出 25 个像素,收集所得的单元格,然后从该单元格开始收集给定数量的单元格。

我并没有真正足够清楚地考虑这种方法,一旦完成就意识到我有一个问题,那就是每次寻找下一个单元格时我正在测试的实际路径都会被破坏,例如

在此处输入图像描述

其中绿色虚线是所需路径,蓝色线是组成每个 nextCell() 打电话,因为支票是从中心进行的每个细胞。

在给定方向上指定距离(像素)有效收集细胞的正确方法是什么?

I have set up a grid class that contains theoretical cells.

I have a method collectCells() which accepts a starting point, radians and distance as arguments. I want this method to return an Array containing all the cells that are along a given angle and that are within the specified distance from the starting point, e.g.

enter image description here

The easiest way for me to do this is just loop through all the pixels in the given direction and pick cells up as I go, something like:

for(var i:int = 0; i<distance; i++)
{
    startPoint.x += Math.cos(radians);
    startPoint.y += Math.sin(radians);

    if( start point falls within uncollected cell) collect cell;
}

This is obviously really poor, as the loop will be as long as the distance I specify - extremely slow.

I tried something different, creating a nextCell() method which accepts the last collected cell and radians. The method threw a point 25 pixels in the direction specified by radians, collected the resulting cell and then started over from that cell for a given amount of cells.

I didn't really think about this approach clearly enough, and once done realized that I had a problem, being that the actual path I'm testing gets broken each time the next cell is sought, e.g.

enter image description here

Where the green dotted line is the desired path, and the blue lines are the paths that make up each nextCell() call because the check is made from the centre of each cell.

What is the correct way to efficiently collect the cells in a given direction over a specified distance (pixels)?

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

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

发布评论

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

评论(1

我做我的改变 2025-01-15 01:21:18

或者您可以使用 Bresenham 画线算法。因为从字面上看,您正在尝试绘制一条像素宽度的线,但像素很大。

Or you could use Bresenham's line drawing algorithm. Because literally you are trying to draw a pixel width line but with huge pixels.

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