如何找到矩形的最小值和最大值?

时间:2018-07-17 13:32:14

标签: javascript python hash 2d rect

我在这里关注这篇文章

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?
        }
    }

}

0 个答案:

没有答案