英文:
Way to stop function from checking number
问题
我有一个函数,基本上检查从i-1
到i+1
以及j
的网格空间,因为它的坐标是(i, j)
。然而,当我尝试检查击中网格的右侧或顶部的网格空间时,它会说它超出范围。我的目标是在代码中添加一些内容,使它能够说if i == some_variable
,然后不要检查它。
b_overlap = False
for i in range(grid_i-1, grid_i+1):
for j in range(grid_j-1, grid_j+1):
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("Ball was added at (" + str(x) + ", " + str(y) + ")")
英文:
I have a function that basically checks grid spaces from like i-1
to i+1
along with j
since its coordinates in the (i,j)
. However, when I go to check a grid space that hits the right or top side of the grid, it says it can't because it's out of range. My goal is to put something in the code for it to say like if i == some_variable
then don't check it.
b_overlap = False
for i in range(grid_i-1, grid_i+1):
for j in range(grid_j-1, grid_j+1):
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("Ball was added at (" + str(x) + ", " + str(y) + ")")
答案1
得分: 1
你可以使用 if
来检查边界,并跳过那些迭代:
b_overlap = False
for i in range(grid_i-1, grid_i+1):
for j in range(grid_j-1, grid_j+1):
# 检查边界
if i < 0 or i >= len(v_circle) or j < 0 or j >= len(v_circle[i]):
continue
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("球已添加在 (" + str(x) + ", " + str(y) + ") 处")
英文:
You can check for boundaries using an if
and skip those iterations:
b_overlap = False
for i in range(grid_i-1, grid_i+1):
for j in range(grid_j-1, grid_j+1):
# Check for boundaries
if i < 0 or i >= len(v_circle) or j < 0 or j >= len(v_circle[i]):
continue
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("Ball was added at (" + str(x) + ", " + str(y) + ")")
答案2
得分: 1
在Python中,range(start, end)
包含 start
但不包含 end
。您可以使用 max()
和 min()
函数来确保不超出范围。
假设 grid_size_i
和 grid_size_j
分别是 i
和 j
维度的最大范围,您可以使用 max(0, grid_i-1)
和 max(0, grid_j-1)
来确保不会低于零(网格的左边和底边),以及 min(grid_i+1, grid_size_i)
和 min(grid_j+1, grid_size_j)
来确保不会超出网格的大小(网格的右边和顶边)。
b_overlap = False
for i in range(max(0, grid_i-1), min(grid_i+1, grid_size_i)):
for j in range(max(0, grid_j-1), min(grid_j+1, grid_size_j)):
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("Ball was added at (" + str(x) + ", " + str(y) + ")")
或者,如果您的网格大小在循环过程中可以更改,您可以使用类似 Blaz 建议的方法,其中您在进行循环时检查每个索引是否有效。
英文:
In Python, range(start, end)
includes start
but excludes end
. You can use max()
and min()
functions to make sure that you're not going out of range.
Assuming grid_size_i
and grid_size_j
are the maximum ranges for i
and j
dimensions, you can use max(0, grid_i-1)
and max(0, grid_j-1)
to make sure you don't go below zero (the left and bottom edges of your grid), and min(grid_i+1, grid_size_i)
and min(grid_j+1, grid_size_j)
makes sure you don't go beyond your grid's size (the right and top edges).
b_overlap = False
for i in range(max(0, grid_i-1), min(grid_i+1, grid_size_i)):
for j in range(max(0, grid_j-1), min(grid_j+1, grid_size_j)):
print("i, j = " + str(i) + ", " + str(j))
if len(v_circle[i][j]) > 0:
for c_iteration in v_circle[i][j]:
if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
b_overlap = True
if not b_overlap:
v_circle[grid_i][grid_j].append(c_candidate)
print("Ball was added at (" + str(x) + ", " + str(y) + ")")
Alternatively, if your grid size can change during the course of the loop, you could use an approach like Blaz suggested where you check whether each index is valid as you go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论