This is being implemented with Python and Pygame but it’s a fairly general programming question (meaning implementation independent).
I have a function which takes as input an x and y integer and should generate a 3×3 grid of neighbouring points (the x and y included).
Note: the 0,0 origin begins at the top left. x increases as you move right, y increases as you move down.
Eg.
def nearest_grid(x, y):
return [[(x-1,y-1),(x,y-1),(x+1,y-1)],[(x-1,y)(x,y),(x+1,y)],[(x-1,y+1),(x,y+1),(x+1,y+1)]]
So, given a grid and a point (marked p) it returns the following as a list of 3 lists:
x x x
x p x
x x x
Is this the most effective / legible way to do this in Python?
EDIT: Suppose I wanted to pass a radius value (where the above radius value would be 1). So, if I passed a radius value of 2 then the above method would quickly become tiresome. Is there a more general way?
1 Answer