开发者

java array with vertices of dodecahedron

开发者 https://www.devze.com 2023-01-31 04:30 出处:网络
I need to create an array of arrays int x[][] = new int[20][3]; where the indexes of x are the vertices of a dodecahedron(just labeled 0-19 since the dodecahedron has 20 vertices), and the element

I need to create an array of arrays

int x[][] = new int[20][3];

where the indexes of x are the vertices of a dodecahedron(just labeled 0-19 since the dodecahedron has 20 vertices), and the elements of x[0-19] are the neighbor vertices. If this is not clear take this sample:

    int y[][] = {{ 1,  5,  4}, { 0,  7,  2}, { 1,  9,  3}, { 2, 11,  4},
                 { 3, 13,  0}, { 0, 14,  6}, { 5, 16,  7}, { 1,  6,  8},
                 { 7,  9, 17}, { 2,  8, 10}, { 9, 11, 18}, {10,  3, 12},
                 {19, 11, 13}, {14, 12,  4}, {13,  5, 15}, {14, 19, 16},
         开发者_如何学Python        { 6, 15, 17}, {16,  8, 18}, {10, 17, 19}, {12, 15, 18}};

In this sample array y[0][0-2] refers to the neighbors of vertex 0, which happen to be 1,5, and 4. In line with this, y[1], y[5], and y[6] contain 0.

I dont want to just write in this array, rather I want to create a random version of this.


If you want to renumber the vertices in a "random" fashion while preserving the dodecahedron vertex structure, then this should do the trick:

    int[] shuffle = // an array containing a permutation of [0..19]
    int[][] z = new int[20][];
    for (int i = 0; i < 20; i++) {
        int[] vy = y[i];
        int[] vz = new int[3];
        for (int j = 0; j < 3; j++) {
            vz[j] = shuffle[vy[j]];
        }
        z[shuffle[i]] = vz;
    }


Create the initial one, like you did, and then start shuffleing it. (lets call it int[][]x -- because you named it it x and y)

Select two different edges by random and swap them, repeat this several times.

If to swap two edges, you have to swap the arrays, as well as the "references" in the arrays. for example: swap edge 1 and 10: psydo code:

int[] temp = x[1];
x[1] = x[10];
x[10] = temp;

for (int i = 0; i < 20; i++) {
  for(int k = 0; k < 3; k++) {
    if (x[i][k]==1) {
       x[i][k]= 10;
    } else if(x[i][k]==10) {
       x[i][k]= 1;
    }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消