I would like to modify my square lattice Python script (this is an agent model for biology) to work in a hexagonal universe.
This is how I create and initialize a 2D matrix in a square model: basically, N is the size of the lattice, and R is the radius of the part of the matrix where I need to change the value at the beginning of the algorithm
a = np.zeros(shape=(N,N)) center = N/2 for i in xrange(N): for j in xrange(N): if( ( pow((i-center),2) + pow((j-center),2) ) < pow(R,2) ): a[i,j] = 1
Then I let the matrix evolve according to certains rules and finally print through the creation of a pickle file:
name = "{0}-{1}-{2}-{3}-{4}.pickle".format(R, A1, A2, B1, B2) pickle.dump(a, open(name,"w"))
Now I would like to do the same, but on a hexagonal grid. I read this interesting StackOverflow question, which clarified how to represent positions on a hexagonal grid with three coordinates, but several things remain unclear as far as I know, i.e.
(a) how should I deal with three axes in Python, believing that what I want is not equivalent to a three-dimensional matrix due to restrictions on coordinates and
(b) how to do it?
Regarding (a), this is what I tried to do:
a = np.zeros(shape=(N,N,N)) for i in xrange(N/2-R, N/2+R+1): for j in xrange(N/2-R, N/2+R+1): for k in xrange(N/2-R, N/2+R+1): if((abs(i)+abs(j)+abs(k))/2 <= 3*N/4+R/2): a[i,j,k] = 1
I find it rather confusing to initialize such an NxNxN matrix, and then find a way to print a subset of it in accordance with the restrictions on the coordinates. I am looking for an easier way and, more importantly, to understand how to build a hexagonal lattice obtained as a result of the algorithm (I donβt know, at the moment I havenβt tried anything).