2

好吧,所以使用元胞自动机的一些基本原理,我设法运行一个程序,生成一组根据规则计算出来的数据。每个单元格都是布尔型的。将元胞自动机数据转换为音乐乐谱(如WolframTones)

当前我将它存储为so - boolean [] [] data - 其中第一个索引是行,第二个是单元格。

现在我已经到了想要将该音乐转换成乐谱(存储为数组)的地步。在the page它显示了如何将来自CA的数据转换图 -

Source

得分数据

Target

我听不太懂如何做到这一点progmatically使用我的存储来完成方案。如果任何人都能帮上忙,那么我可以在必要时提供更多信息。

回答

1

映射看起来直截了当:

target[x, y] = source[OFFSET - y, x] 

其中OFFSET最后行的索引拷贝(33如果我算右)。

你的实现可以使用两个嵌套循环来复制数组。


编辑:

这是你的转换可能是什么样子:

public class Converter 
{ 
    public static boolean[][] convert(boolean[][] source, int offset, int width) 
    { 
     final boolean[][] target = new boolean[width][source.length]; 

     for(int targetRow=0 ; targetRow<width ; targetRow++) 
     { 
      for(int targetCol=0 ; targetCol<source.length ; targetCol++) 
      { 
       target[targetRow][targetCol] = source[targetCol][offset + width-1 - targetRow]; 
      } 
     } 

     return target; 
    } 
} 

这是下面的测试代码(原阵列和转换阵列)的输出使用偏移量为2(前两行省略)和宽度为7(转换了7列):

 █  
    ███  
    ██ █ 
    ██ ████ 
██ █ █ 
██ ████ ███ 

    █ █ 
    ██ 
█ █ █ 
██ ███ 
██ █ 
    ██ █ 
    ██ 

测试代码是源阵列的方向,内容和输出字符串转换定义:

public class ConverterTest 
{ 
    private final static int OFFSET = 2; 
    private final static int WIDTH = 7; 

    private static final String[] sourceString = { 
     "  █  ", 
     " ███ ", 
     " ██ █ ", 
     " ██ ████ ", 
     " ██ █ █ ", 
     "██ ████ ███", 
    }; 


    public static void main(String[] args) 
    { 
     final boolean[][] source = getSourceArray(); 
     printArray(source); 
     final boolean[][] target = Converter.convert(source, OFFSET, WIDTH); 
     printArray(target); 
    } 


    private static boolean[][] getSourceArray() 
    { 
     final boolean[][] sourceArray = new boolean[sourceString.length][sourceString[0].length()]; 

     for(int row=0 ; row<sourceString.length ; row++) 
     { 
      for(int col=0 ; col<sourceString[0].length() ; col++) 
      { 
       sourceArray[row][col] = (sourceString[row].charAt(col) != ' '); 
      } 
     } 

     return sourceArray; 
    } 


    private static void printArray(boolean[][] arr) 
    { 
     for(int row=0 ; row<arr.length ; row++) 
     { 
      for(int col=0 ; col<arr[0].length ; col++) 
      { 
       System.out.print(arr[row][col] ? '█' : ' '); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 
} 
+0

对不起,我不明白你的答案。你能否进一步阐述(x,y和OFFSET代表什么)。我读了你的抵消定义,我真的不明白这一点。 – liamzebedee 2012-03-28 10:11:18

+0

@ LiamE-p:编辑我的答案,包括一些示例代码。希望能解释我的意思。 – 2012-03-28 18:47:33

+0

@ LiamE-p:我的回答有帮助吗? – 2012-04-06 09:09:47