Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用从中心点开始连续递减的数字填充二维数组_Java_Algorithm - Fatal编程技术网

Java 如何使用从中心点开始连续递减的数字填充二维数组

Java 如何使用从中心点开始连续递减的数字填充二维数组,java,algorithm,Java,Algorithm,我有一个二维阵列,光源放在它的坐标上。此光源附加了一个亮度值。我想知道我该如何使这盏灯照亮它周围的瓷砖,距离等于它的亮度,每个被照亮的瓷砖的亮度等于它与光源的距离。例如,亮度为5的灯泡将以5个单元为半径照亮,距离2步的单元的亮度为4,距离5步的单元的亮度为1。如何在二维阵列中设置值 final int SIZE = 30; // size of array int[][] brightnessOfTiles = new int[SIZE][SIZE]; brightnessOfTiles[x]

我有一个二维阵列,光源放在它的坐标上。此光源附加了一个亮度值。我想知道我该如何使这盏灯照亮它周围的瓷砖,距离等于它的亮度,每个被照亮的瓷砖的亮度等于它与光源的距离。例如,亮度为5的灯泡将以5个单元为半径照亮,距离2步的单元的亮度为4,距离5步的单元的亮度为1。

如何在二维阵列中设置值

final int SIZE = 30; // size of array
int[][] brightnessOfTiles = new int[SIZE][SIZE];

brightnessOfTiles[x][y] = 1234; // some value
for(int x = 0; x < SIZE; x++){
    for(int y = 0; y < SIZE; y++){

        brightnessOfTiles[x][y] = ...; // see above
    }
}
如何确定特定位置的灯光值

int centerX = 15; // x coordinate of position of light source
int centerY = 10; // y coordinate of position of light source
float dropoffFactor = 1; // how fast the brightness fades
brightnessOfTiles[x][y] = MAX_BRIGHTNESS / (1 + dropoffFactor * getDistanceSquaredBetweenPoints(x, y, centerX, centerY));
如何确定两点之间的距离

private int getDistanceSquaredBetweenPoints(x, y, centerX, centerY){

    // formula is: distance = sqrt( (x - cx) ^ 2 + (y - cy) ^ 2 )
    //   so, distance ^ 2 = (x - cx) ^ 2 + (y - cy) ^ 2

    return Math.pow(centerX - x, 2) + Math.pow(centerY - y, 2);
}
如何迭代二维数组

final int SIZE = 30; // size of array
int[][] brightnessOfTiles = new int[SIZE][SIZE];

brightnessOfTiles[x][y] = 1234; // some value
for(int x = 0; x < SIZE; x++){
    for(int y = 0; y < SIZE; y++){

        brightnessOfTiles[x][y] = ...; // see above
    }
}

我投票将这个问题作为离题题来结束,因为。对不起,我没有具体说明我以前尝试过的东西。顺便说一下,这不是家庭作业,只是个人项目。在Matlab中工作了很长时间后,我正试图更加熟悉Java。我想一个更好的问题应该是关于两点之间的距离,你帮了我。所以非常感谢。如果这解决了你的问题,请也投票给我答案。恐怕我没有足够高的声誉。等我回来的时候,我会回来的。