gpt4 book ai didi

java - 生成一个接近中心的值较小的矩阵

转载 作者:行者123 更新时间:2023-11-29 04:41:02 24 4
gpt4 key购买 nike

我希望生成一个整数矩阵,其中整数在边缘处较大,如下所示:

[23] [23] [23] [23] [23] 
[15] [15] [15] [15] [15]
[10] [10] [10] [10] [10]
[10] [7] [7] [7] [10]
[10] [4] [4] [4] [10]
[10] [3] [3] [3] [10]
[10] [3] [2] [3] [10]
[10] [3] [1] [3] [10]
[10] [3] [1] [3] [10]
[10] [3] [1] [3] [10]
[10] [3] [2] [3] [10]
[10] [3] [3] [3] [10]
[10] [4] [4] [4] [10]
[10] [7] [7] [7] [10]
[10] [10] [10] [10] [10]
[15] [15] [15] [15] [15]
[23] [23] [23] [23] [23] `

到目前为止一切顺利!但是,我遇到的问题是保持顶部和底部的最大边缘值与左右边缘相同,如下所示:

[23] [23] [23] [23] [23] 
[23] [15] [15] [15] [23]
[23] [10] [10] [10] [23]
[23] [7] [7] [7] [23]
[23] [7] [4] [7] [23]
[23] [7] [3] [7] [23]
[23] [7] [2] [7] [23]
[23] [7] [2] [7] [23]
[23] [7] [2] [7] [23]
[23] [7] [2] [7] [23]
[23] [7] [2] [7] [23]
[23] [7] [3] [7] [23]
[23] [7] [4] [7] [23]
[23] [7] [7] [7] [23]
[23] [10] [10] [10] [23]
[23] [15] [15] [15] [23]
[23] [23] [23] [23] [23]

注意边都等于相同的最大数字 23。让它们都等于相同的数字是我遇到问题的地方(这是由于不同的长度和宽度)。以下是我生成矩阵的方式:

public void generateChanceMatrix(){
double ratio = numPointsX/numPointsY; //The difference in length of x and y

for (int i = 0; i < numPointsX; i++){ //for each point
for (int j = 0; j < numPointsY; j++){

double max = Math.abs((numPointsX /2) - i); // these are responsible for generating larger numbers as you approach the edge
double max2 = Math.abs((numPointsY /2) - j)* ratio; //this ratio is how i try to even out the difference in length of x and y

if (max2 > max){ //see if the y axis is larger than the x, choose the larger
max = max2;
}

chanceMatrix[i][j] = (int) Math.pow(2.2, max/2); //make it exponential instead of linear
}
}
}

我如何调整它以确保所有边缘的值接近相同的最大值?对于那些好奇的人,我正在使用它来生成一个 map 边界被封锁的游戏世界,见下文:

Ships flying around the world!

提前致谢!

最佳答案

我会根据到边缘的相对距离使用一些插值。 IE。相对距离 t 在矩阵中间为零,在边缘为 1。然后你可以根据这个值做简单的插值:

double relativeDiffX = 2 * abs(i - numPointsX / 2) / (numPointsX - 1);
double relativeDiffY = 2 * abs(j - numPointsY / 2) / (numPointsY - 1);
double t = max(relativeDiffX, relativeDiffY);
//now any interpolation
chanceMatrix[i][j] = (int) Math.pow(2.2, t); //this will produce values between 1 and 2.2

关于java - 生成一个接近中心的值较小的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39182517/

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