我有一个2D阵列,其中有一个“光源”放在它的坐标上。该光源附有亮度值。我想知道我是怎么做到这样的,这个灯照亮它周围的瓷砖到一个等于它的亮度的距离,每个照明的瓷砖亮度等于它与光源的距离。例如,亮度为5的灯泡将照射半径为5个单元格,单元格距离为2步距离,亮度为4,距离为5步距离为1。
答案 0 :(得分:0)
如何在2D数组中设置值?
final int SIZE = 30; // size of array
int[][] brightnessOfTiles = new int[SIZE][SIZE];
brightnessOfTiles[x][y] = 1234; // some value
如何确定特定地点的灯光值?
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);
}
你将如何迭代2D数组?
for(int x = 0; x < SIZE; x++){
for(int y = 0; y < SIZE; y++){
brightnessOfTiles[x][y] = ...; // see above
}
}