首页 > 解决方案 > 如何找到矩形的最小值和最大值?

问题描述

我在这里关注这篇文章:

https://www.gamedev.net/articles/programming/general-and-gameplay-programming/spatial-hashing-r2697

创建我的空间散列函数来解决我的 2D 游戏中碰撞检测中的缓慢问题。但我不明白插入对象的部分:

如何找到下面对象的最小值和最大值?

myObject = {
    x: 704,
    y: 448,
    width: 32,
    height: 32
}

几年前我放弃了python,忘记了它的语法,下面的代码是什么意思:

def insert_object_for_box(self, box, object):

# hash the minimum and maximum points

min, max = self._hash(box.min), self._hash(box.max)

# iterate over the rectangular region

for i in range(min[0], max[0]+1):

for j in range(min[1], max[1]+1):

# append to each intersecting cell

self.contents.setdefault( (i, j), [] ).append( object )

这会在javascript中吗:

function insert_object_for_box(box, object) {

    box.min.x = here?;
    box.min.y = here?;
    box.max.x = here?;
    box.max.y = here?;

    var minX = this.hash(box.min.x);
    var minY = this.hash(box.min.y);

    var maxX = this.hash(box.max.x);
    var maxY = this.hash(box.max.y);

    for(var x = minX; x < maxX+1, x++) {
        for(var y = minY; y < maxY+1, y++) {
             here?
        }
    }

}

标签: javascriptpythonhash2drect

解决方案


推荐阅读