停止函数检查数字的方法

huangapple go评论52阅读模式
英文:

Way to stop function from checking number

问题

我有一个函数,基本上检查从i-1i+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(&quot;i, j = &quot; + str(i) + &quot;, &quot; + str(j))
        if len(v_circle[i][j]) &gt; 0:
            for c_iteration in v_circle[i][j]:
                if distance(c_candidate, c_iteration) &lt; c_iteration.radius_ + c_candidate.radius_:
                    b_overlap = True
if not b_overlap:
    v_circle[grid_i][grid_j].append(c_candidate)
    print(&quot;Ball was added at (&quot; + str(x) + &quot;, &quot; + str(y) + &quot;)&quot;)

答案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 &lt; 0 or i &gt;= len(v_circle) or j &lt; 0 or j &gt;= len(v_circle[i]):
            continue

        print(&quot;i, j = &quot; + str(i) + &quot;, &quot; + str(j))
        if len(v_circle[i][j]) &gt; 0:
            for c_iteration in v_circle[i][j]:
                if distance(c_candidate, c_iteration) &lt; c_iteration.radius_ + c_candidate.radius_:
                    b_overlap = True
if not b_overlap:
    v_circle[grid_i][grid_j].append(c_candidate)
    print(&quot;Ball was added at (&quot; + str(x) + &quot;, &quot; + str(y) + &quot;)&quot;)

答案2

得分: 1

在Python中,range(start, end) 包含 start 但不包含 end。您可以使用 max()min() 函数来确保不超出范围。

假设 grid_size_igrid_size_j 分别是 ij 维度的最大范围,您可以使用 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(&quot;i, j = &quot; + str(i) + &quot;, &quot; + str(j))
        if len(v_circle[i][j]) &gt; 0:
            for c_iteration in v_circle[i][j]:
                if distance(c_candidate, c_iteration) &lt; c_iteration.radius_ + c_candidate.radius_:
                    b_overlap = True
if not b_overlap:
    v_circle[grid_i][grid_j].append(c_candidate)
    print(&quot;Ball was added at (&quot; + str(x) + &quot;, &quot; + str(y) + &quot;)&quot;)

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.

huangapple
  • 本文由 发表于 2023年6月16日 01:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484043.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定