Saturday, December 27, 2014

Dimensions

Yesterday I had an idea of making a generalized p-dimensional m,n,k game (tic-tac-toe). The first problem is understanding the (spatial) dimensions. How can I check the neighbours of a point in space? How many directions should I check?

Let x∈ℜp, x = (x1, x2,...xp). Then a neighbouring point in direction d∈ℜp, where di∈{-1, 0, 1} and d  0 is simply x+d. Here the maximum-norm of d is 1. Then if you take a point then the total number of directions you should check is
(3p-1)/2 
(we can consider the p=ei case the duplicate of p=-ei where ei is the i-th unit vector - e.g. right and left are the same directions, same for all duplicate directions p=-p, also we don't count the zero-vector).

Example: 2D table: horizontal, vertical and right-diagonal and left-diagonal. ((32-1)/2=4).
Same for 3D: (33-1)/2=13:
  • (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0), (1,1,1) =>  7 (forward, up, right and combinations)
  • (0,-1,1), (1,0,-1), (-1,1,0), (1,-1,1), (1,-1,-1), (1,1,-1) =>   6 (negating one index, and combinations)
So, I know how many directions I have for a game. But how to store the p-dimensional table? C# has multidimensional arrays, but I don't know the number of dimensions in advance. So I decided to make a single array of it.

In 2D, you access an element in case of a 2D array: arr[x, y], or with a single-dim array arr[x+y*sizex]. 3D case: arr[x+y*sizex+z*sizex*sizey]. Following this pattern I can calculate the linear offset of a p-dim array with size vector S∈ℜp at location x in the form:
xTs, where sT= (1, S1, S1*S2, S1*S2*S3,...,∏Si)
It can be simply rolled in a loop:

LinearOffset(x, dims)
    s := 1
    r := 0
    for i from 0 to count(dims)
        r := r + s*xi
        s := s * dimsi
    end for
    return r

    It's all good, now I can create a p-dim array, access the neighbours, and write the tic-tac-toe. But how to visualize it? 2D is easy. 3D is easy too. I saw a video on YouTube with a 4D hypercube represented as 8 3D cubes. So, we can split up dimensions. A 3*3*3*3 4D tic-tac-toe is simplified to 3 33 3D tic-tac-toes. The last array (dimension) index is the index of the 3D tic-tac-toe table. A 35 tic-tac-toe is simplified to 3 4D, then to 9 3D tic-tac-toes, and so on.

No comments:

Post a Comment