获取索引序列

时间:2018-11-15 07:57:32

标签: java arrays multidimensional-array

我有点问题。任务是拍摄照片,通过EdgeDetector运行它,并使用3轴矩阵打印机绘制边缘。

我大部分都被覆盖了,但仍然存在一个主要障碍。当我通过Sobel运行图片并使用二进制截断时,它将返回一个0和255的二维int数组。只是一个更好理解的测试数组示例。

Point::Point(int x = 0, int y = 0);
Point::Point(int n);

然后,我使用嵌套的for循环遍历数组,并为我提供Point::Point(); // default Point::Point(int x, int y); Point::Point(int n); 坐标,255 255 255 255 255 255 255 255 255 0 0 0 255 0 0 255 255 255 0 0 0 255 255 255 255 0 0 255 255 0 0 255 255 255 255 0 255 0 0 255 255 0 255 0 0 0 0 255 255 255 255 255 255 255 255 255 坐标和数组中每个位置的大小,就像这样。我正在使用StringBuilder,因为我需要将字符串发送到PLC。

i

我还想出了如何打印这样的连接元素的元素。这是按行的,但是有按列的方法,左右对角线。再次将其放入StringBuilder。

j

现在是问题所在。我非常希望能够将字符串发送到仅包含假定绘制坐标的PLC。在示例数组中,我将是:

  

0,0至0,7起

     

1,0起; 1,1至1,3下降; 1,4起; 1,5至1,6下跌; 1,7起

     

2,0至2,1向上; 2,2至2,4下降; 2,5至2,7起

     

3,0起; 3,1至3,2下降; 3,3至3,4起; 3,5至3,6下跌; 3,7起

     

以此类推。

从本质上讲,在数组中间获得了一系列元素。

其背后的想法是, public static void stringBuilder(int m[][]) { // start method stringbuilder StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < m.length ; i++) { // start i for loop for ( int j = 0 ; j < m[i].length ; j++) { // start j for loop int magnitude = m[j][i]; /* sb.append("("); sb.append(i); sb.append(","); sb.append(j); sb.append(")"); sb.append(" : "); */ if (magnitude == 0) { sb.append(magnitude); sb.append(" "); } else { sb.append(magnitude); sb.append(" "); } sb.append(" "); } // end j for loop sb.append("\n"); } // end i for loop System.out.println(sb); } // end method stringbuilder 坐标中的差异可以转换为步进电机中的多个步骤。

我只是不知道怎么做。

2 个答案:

答案 0 :(得分:0)

逐行读取并检查值何时更改,您可以轻松地对其进行管理。

这是一个例子:

public static void main(String[] args) {
    int[][] array = {
            { 255, 255, 255, 255, 255, 255, 255, 255 },
            { 255, 0, 0, 0, 255, 0, 0, 255 },
            { 255, 255, 0, 0, 0, 255, 255, 255 },
            { 255, 0, 0, 255, 255, 0, 0, 255 },
            { 255, 255, 255, 0, 255, 0, 0, 255 },
            { 255, 0, 255, 0, 0, 0, 0, 255 },
            { 255, 255, 255, 255, 255, 255, 255, 255 }
            };
    StringBuilder result = new StringBuilder();
    int currentValue;
    int startIndex;
    for (int i = 0; i < array.length; i++) {
        if (array[i].length == 0) {
            continue;
        }
        currentValue = array[i][0];
        startIndex = 0;
        for (int j = 1; j < array[i].length; j++) {
            if (currentValue != array[i][j]) {
                updateResult(result, currentValue, i, startIndex, j - 1);
                currentValue = array[i][j];
                startIndex = j;
            }
        }
        updateResult(result, currentValue, i, startIndex, array[i].length - 1);
        result.append("\n");
    }
    System.out.println(result.toString());
}

private static void updateResult(StringBuilder result, int value, int row, int startIndex, int endIndex) {
    if (startIndex == endIndex) {
        addCoordinates(result, row, startIndex);
    } else {
        addCoordinates(result, row, startIndex);
        result.append(" to ");
        addCoordinates(result, row, endIndex);
    }
    result.append(" ");
    switch(value) {
    case 255:
        result.append("up");
        break;
    case 0:
        result.append("down");
        break;
        default:
            System.err.println("Unrecognized value " + value);
    }
    result.append("; ");
}

private static void addCoordinates(StringBuilder result, int row, int column) {
    result.append(row);
    result.append(",");
    result.append(column);
}

对于您的输入数组,结果将是:

0,0 to 0,7 up; 
1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up; 
2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up; 
3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up; 
4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up; 
5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up; 
6,0 to 6,7 up; 

答案 1 :(得分:0)

尝试以下代码:

// String formatting
public static final String FORMAT_TO_UP = "%d,%d to %d,%d up";
public static final String FORMAT_TO_DOWN = "%d,%d to %d,%d down";

public static final String FORMAT_UP = "%d,%d up";
public static final String FORMAT_DOWN = "%d,%d down";

public static void stringBuilder(int m[][])
{ // start method stringbuilder
    StringBuilder sb = new StringBuilder();

    for(int i = 0; i < m.length; i++)
    { // start i for loop

        int upStart = 0;
        int downStart = 0;

        boolean hasPreviousUp = false;
        boolean hasPreviousDown = false;

        final int rowSize = m[i].length;
        for(int j = 0; j < rowSize; j++)
        { // start j for loop
            int magnitude = m[i][j];

            // Reached a down
            if(magnitude == 0)
            {
                // Handle the change from up to down
                if(hasPreviousUp)
                {
                    // If range is only 1
                    if(upStart == j - 1)
                    {
                        sb.append(String.format(FORMAT_UP, i, j - 1));
                    }
                    else
                    {
                        sb.append(String.format(FORMAT_TO_UP, i, upStart, i, j - 1));
                    }
                    hasPreviousUp = false;
                    sb.append("; ");
                }

                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousDown)
                {
                    hasPreviousDown = true;
                    downStart = j;
                }
            }
            // Reached an up
            else
            {
                // Handle the change from down to up
                if(hasPreviousDown)
                {
                    // If range is only 1
                    if(downStart == j - 1)
                    {
                        sb.append(String.format(FORMAT_DOWN, i, j - 1));
                    }
                    else
                    {
                        sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, j - 1));
                    }
                    hasPreviousDown = false;
                    sb.append("; ");
                }

                // Don't reset the start value if there was no change of up/down
                if(!hasPreviousUp)
                {
                    hasPreviousUp = true;
                    upStart = j;
                }
            }

        } // end j for loop

        // Handle the last up/down range
        if(hasPreviousUp)
        {
            if(upStart == rowSize - 1)
            {
                sb.append(String.format(FORMAT_UP, i, rowSize - 1));
            }
            else
            {
                sb.append(String.format(FORMAT_TO_UP, i, upStart, i, rowSize - 1));
            }
        }
        else if(hasPreviousDown)
        {
            if(downStart == rowSize - 1)
            {
                sb.append(String.format(FORMAT_DOWN, i, rowSize - 1));
            }
            else
            {
                sb.append(String.format(FORMAT_TO_DOWN, i, downStart, i, rowSize - 1));
            }
        }
        sb.append("\n");

    } // end i for loop

    System.out.println(sb);
} // end method stringbuilder

结果:

0,0 to 0,7 up
1,0 up; 1,1 to 1,3 down; 1,4 up; 1,5 to 1,6 down; 1,7 up
2,0 to 2,1 up; 2,2 to 2,4 down; 2,5 to 2,7 up
3,0 up; 3,1 to 3,2 down; 3,3 to 3,4 up; 3,5 to 3,6 down; 3,7 up
4,0 to 4,2 up; 4,3 down; 4,4 up; 4,5 to 4,6 down; 4,7 up
5,0 up; 5,1 down; 5,2 up; 5,3 to 5,6 down; 5,7 up
6,0 to 6,7 up