打破象限

时间:2015-11-20 13:57:50

标签: java recursion

所以假设我有一个10x10的块,我的目标是将其打入象限,然后一旦我完成了这个,回到第一个象限并打破四个象限,转到第二个象限并打破进入四个象限,依此类推,直到它们全部被破坏,然后回到第一个并重复它为新的一组块。

所以基本上我想要将一个块分成4块并将每个新块分成四块并继续使用这种模式。

这是我到目前为止的代码:

private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg) {

        int red = 0;
        int green = 0;
        int blue = 0;
        int rgb = 0;

        if(endHeight <= 1 || endWidth <= 1) {
            return;
        }

        // get average
        for(int i = startHeight; i < endHeight; i++) {
            for(int j = startWidth; j < endWidth; j++) {
                rgb = img.getRGB(j, i);
                red += (rgb >> 16) & 0xFF;
                green += (rgb >> 8) & 0xFF;
                blue += (rgb) & 0xFF;
            }
        }

        // get average color
        red = red /((startWidth - endWidth) * (startHeight - endHeight));
        green = green/((startWidth - endWidth) * (startHeight - endHeight));
        blue = blue/((startWidth - endWidth) * (startHeight - endHeight));
        Color color = new Color(red,green,blue);

        // apply 
        for(int i = startHeight; i < endHeight; i++) {
            for(int j = startWidth; j < endWidth; j++) {
                blockImg.setRGB(j, i, color.getRGB());
            }
        }

        getBlockAverage(startHeight, endHeight/2, startWidth, endWidth/2, img, blockImg);
        getBlockAverage(endHeight/2+1, endHeight, endWidth, endWidth/2, img, blockImg);
        getBlockAverage(startHeight, endHeight/2, endWidth/2+1, endWidth, img, blockImg);
        getBlockAverage(endHeight/2+1, endHeight, endWidth/2+1, endWidth, img, blockImg);


    }

所以我正在尝试递归调用此函数,该函数将继续将每个块分解为象限,但我继续得到堆栈溢出。

我的代码所做的是获取图像,获取该块的平均颜色并显示它。这是一个相对简单的概念,我将稍微调整一下以获得一些很酷的图像,但是现在我想解决这个问题。

此处编辑是System.out.println(startHeight + " " + endHeight + " "+ startWidth + " " + endWidth);

的结果
0 72 0 108
0 36 0 54
0 18 0 27
0 9 0 13
0 4 0 6
0 2 0 3
0 1 0 1
0 1 2 3
3 4 0 3
3 4 0 1
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3

然后重复3 4 2 4直到我得到堆栈溢出。

1 个答案:

答案 0 :(得分:4)

进行递归时,有三个功能很重要:

  1. 破裂条件
  2. 实际工作量
  3. 整理递归结果
  4. 具体做法是:

    private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg) {
        // break recursion on empty block
        if(endHeight <= startHeight || endWidth <= startWidth)
            return;
    
        if(startHeight + 1 == endHeight || startWidth + 1 == endWidth) {
            // work on single columns or rows of pixels
            // results are stored to blockImg...
        } else {
            // recurse
            getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg);
            getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg);
            getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth + endWidth)/2, endWidth, img, blockImg);
            getBlockAverage((startHeight + endHeight)/2, endHeight, (startWidth + endWidth)/2, endWidth, img, blockImg);
            // now collate the results in blockImg...
        }
    }