javascript - 如何找到矩形的最小值和最大值?
问题描述
我在这里关注这篇文章:
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?
}
}
}
解决方案
推荐阅读
- python - 如何优化此代码,因为此代码通过 Time limit exceeded ,不同测试用例的运行时错误?
- wechat - 如何为微信小程序制作联系表
- asp.net-core - 具有多个身份验证方案的 SecurityTokenSignatureKeyNotFoundException
- azure - ServiceBusReceivedMessage 的单元测试
- html - 为什么我的导航栏按钮不起作用(我只是一个初学者,对不起......)
- microsoft-graph-api - Microsoft 图形标题值“HonorNonIndexedQueriesWarningMayFailRandomly”
- mongodb - 如何在 mongodb 中更新此订单状态?这里一个订单 ID 和产品在一个数组中,并且在产品对象内部它包含状态
- node.js - 如何将前端组件中的数据格式化为节点 js 中的特定格式?
- python - 使用 seaborn 绘制具有实际值与预测值的散点图
- java - Spring Data MongoDB:类型不能为空