gpt4 book ai didi

java - 我怎样才能以更紧凑的方式在java中获取颜色编号

转载 作者:行者123 更新时间:2023-12-01 11:47:40 25 4
gpt4 key购买 nike

这是我的代码部分。我正在寻找一种方法来减少红色、绿色和蓝色三个大部分的冗余,但看起来类似的计算。有更好的方法让它发挥作用吗?对于循环?或者有什么想法吗?有人可以给我一个想法吗?请!!!!!谢谢!!!

for(int row = 1; row < pi.getHeight() - 1; row++) {
for(int col = 1; col < pi.getWidth() - 1; col++) {
int red_P_C = 4 * data[row][col].red;
int red_P_1 = 1 * data[row-1][col-1].red;
int red_P_2 = 2 * data[row-1][col].red;
int red_P_3 = 1 * data[row-1][col+1].red;
int red_P_4 = 2 * data[row][col-1].red;
int red_P_5 = 2 * data[row][col+1].red;
int red_P_6 = 1 * data[row+1][col-1].red;
int red_P_7 = 2 * data[row+1][col].red;
int red_P_8 = 1 * data[row+1][col+1].red;
int redTotal = red_P_C + red_P_1 + red_P_2 + red_P_3 + red_P_4 + red_P_5 + red_P_6 + red_P_7 + red_P_8;
int redActual = redTotal/16;

int g_P_C = 4 * data[row][col].green;
int g_P_1 = data[row-1][col-1].green;
int g_P_2 = 2 * data[row-1][col].green;
int g_P_3 = data[row-1][col+1].green;
int g_P_4 = 2 * data[row][col-1].green;
int g_P_5 = 2 * data[row][col+1].green;
int g_P_6 = data[row+1][col-1].green;
int g_P_7 = 2 * data[row+1][col].green;
int g_P_8 = data[row+1][col+1].green;

int gTotal = g_P_C + g_P_1 + g_P_2 + g_P_3 + g_P_4 + g_P_5 + g_P_6 + g_P_7 + g_P_8;
int gActual = gTotal/16;

int blu_P_C = data[row][col].blue * 4;
int blu_P_1 = data[row-1][col-1].blue;
int blu_P_2 = 2 * data[row-1][col].blue;
int blu_P_3 = data[row-1][col+1].blue;
int blu_P_4 = 2 * data[row][col-1].blue;
int blu_P_5 = 2 * data[row][col+1].blue;
int blu_P_6 = data[row+1][col-1].blue;
int blu_P_7 = 2 * data[row+1][col].blue;
int blu_P_8 = data[row+1][col+1].blue;

int bluTotal = blu_P_C + blu_P_1 + blu_P_2 + blu_P_3 + blu_P_4 + blu_P_5 + blu_P_6 + blu_P_7 + blu_P_8;
int bluActual = bluTotal/16;

newData[row][col].red = redActual;
newData[row][col].green = gActual;
newData[row][col].blue = bluActual;

最佳答案

您可以创建一个 naybor 偏移量数组。简化:

int[][] nayborOffsets = new int[][] {
{ -1, -1 }, { 0, -1 }, { 1, -1 },
{ -1, 0 }, { 1, 0},
{ -1, 1 }, { 0, 1 }, { 1, 1 }
};

MyColor[] nayborRGBs = new MyColor[nayborOffsets.length];
for (int i = 0; i < nayborOffsets.length; ++i) {
int[] offsets = nayborOffsets[i];
nayborRGBs[i] = data[row + offsets[0]][col + offsets[1]];
}

其中 MyColor 应该是 data 元素的类。

关于java - 我怎样才能以更紧凑的方式在java中获取颜色编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29026814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com