Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

python 3.x - Simple square grid lookup

I'm drawing a blank on trying to come up with a simple square grid lookup in python and hope someone can give me some pointers here. Let's say I have a 5x5 grid, starting from the center (3,3), I'd like the lookup to step up the radius outputting the peripheral coordinates like this:

 Radius     Output   
   0        [[3,3]]   
   1        [[2,2], [2,3], [2,4], [3,2], [3,4], [4,2], [4,3], [4,4]]   
   2        [[1,1], [1,2], [1,3], [1,4], [1,5], [2,1], [2,5], [3,1], 
             [3,5], [4,1], [4,5], [1,1], [1,2], [1,3], [1,4], [1,5]]

Any pointers will be greatly appreciated!

UPDATE: The code I have at the moment is:

center_coordinate = [3, 3]
completed_locations = [center_coordinate]
radius = 0
max_radius = 3
while radius != max_radius:
    x_grid = [x for x in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
    y_grid = [y for y in range(center_coordinate[0] - radius, center_coordinate[0] + radius + 1)]
    locations = []
    for x in x_grid:
        for y in y_grid:
            if [x, y] not in completed_locations:
                locations.append([x, y])
                completed_locations.append([x, y])
    radius += 1
    print(radius, locations)

While this does do the job, I am looking for a solution that wouldn't require me to cross check each location as it iterates through.. the actual grid size i'll be working against is 750x750 and this particular module will be called on regularly.

question from:https://stackoverflow.com/questions/65858565/simple-square-grid-lookup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For such kind of problem where you traverse through grids to form circle then Midpoint circle algorithm is a possibility

For example to demonstrate you can use something like this according to your need to implement your requirement

def midPointCircleDraw(x_centre, y_centre, r): 
    x = r 
    y = 0
    
    print("(", x + x_centre, ", ",  
               y + y_centre, ")",  
               sep = "", end = "")  
    
    
    if (r > 0) : 
      
        print("(", x + x_centre, ", ", 
                  -y + y_centre, ")",  
                  sep = "", end = "")  
        print("(", y + x_centre, ", ",  
                   x + y_centre, ")", 
                   sep = "", end = "")  
        print("(", -y + x_centre, ", ",  
                    x + y_centre, ")", sep = "")  
      
     
    P = 1 - r  
  
    while x > y: 
      
        y += 1
          
       
        if P <= 0:  
            P = P + 2 * y + 1
              
         
        else:          
            x -= 1
            P = P + 2 * y - 2 * x + 1
          
        if (x < y): 
            break
           
        print("(", x + x_centre, ", ", y + y_centre, 
                            ")", sep = "", end = "")  
        print("(", -x + x_centre, ", ", y + y_centre,  
                             ")", sep = "", end = "")  
        print("(", x + x_centre, ", ", -y + y_centre, 
                             ")", sep = "", end = "")  
        print("(", -x + x_centre, ", ", -y + y_centre, 
                                        ")", sep = "")  
          
         
        if x != y: 
          
            print("(", y + x_centre, ", ", x + y_centre,  
                                ")", sep = "", end = "")  
            print("(", -y + x_centre, ", ", x + y_centre, 
                                 ")", sep = "", end = "")  
            print("(", y + x_centre, ", ", -x + y_centre, 
                                 ")", sep = "", end = "")  
            print("(", -y + x_centre, ", ", -x + y_centre,  
                                            ")", sep = "")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...