代码之家  ›  专栏  ›  技术社区  ›  Georgia Nissen

检查两个列表的圆重叠

  •  0
  • Georgia Nissen  · 技术社区  · 2 年前

    我编写这段代码的目标是根据用户输入放置两个大小的圆圈,以创建随机放置的圆圈的非重叠显示。从现在开始,我可以很好地放置大圆圈。该代码的基础是将其拆分为网格空间,检查周围的网格空间是否有圆,然后检查是否有重叠。 对于较小的圆圈,我想检查包含较小圆圈的列表,如果没有重叠,请使用网格思想再次检查较大圆圈的列表。我现在的代码没有将任何圆圈放入较小的圆圈列表中,因此结果不会将任何圆圈打印到网格上。

    #Small grid information
    sgrid_minw = 6*smallradius
    sgrid_minh = 6*smallradius
    
    n_smgridX = math.floor(1/sgrid_minw)
    n_smgridY = math.floor(1/sgrid_minh)
    
    actualsmgrid_w = 1/n_smgridX
    actualsmgrid_h = 1/n_smgridY
    
    v_smcircle = [[ [] for _ in range (n_smgridX)] for _ in range(n_smgridY)]
    print(v_smcircle)
    n_addsmall = 1
    small_circles = 0
    while n_addsmall > 0:
    
    n_addsmall = 0
    for _ in range(1000):
      smallr = smallradius
      x = random.uniform(smallr, 1-smallr)
      y = random.uniform(smallr, 1-smallr)
      c_candidate = Circle(x, y, smallr)
    
      grid_i = math.floor(x/actualgrid_w)
      grid_j = math.floor(y/actualgrid_h)
    
      grid_smi = math.floor(x/actualsmgrid_w)
      grid_smj = math.floor(x/actualsmgrid_h)
    
      #print("Candidate circle pos and grid : ("+str(x)+", "+str(y)+",["+str(grid_smi)+", "+str(grid_smj)+"]")
    
      small_check = False
      for i in range(max(0, grid_smi-1), min(grid_smi+2, n_smgridX)):
        for j in range(max(0, grid_smj-1), min(grid_smj+2, n_smgridY)):
          if len(v_smcircle[i][j]) > 0:
            for c_iteration in v_smcircle[i][j]:
              if distance(c_candidate, c_iteration) < c_iteration.radius_ + c_candidate.radius_:
                small_check = True
      big_check = False
      if not small_check:
        for i in range(max(0, grid_i-1), min(grid_i+2, n_gridX)):
          for j in range(max(0, grid_j-1), min(grid_j+2, n_gridY)):
            if len(v_circle[i][j]) > 0:
              for big_circle in v_circle[i][j]:
                if distance(big_circle, c_candidate) >= big_circle.radius_ + c_candidate.radius_:
                  big_check = True
      if not big_check:
        v_smcircle[grid_smi][grid_smj].append(c_candidate)
        n_addsmall = n_addsmall + 1
        small_circles += 1
    
    0 回复  |  直到 2 年前