0
我理解这个概念,但我在实现2D细分背后的循环逻辑时遇到了困难。我有一个二维数组代表一个网格的角落播种。我相信我需要3个循环。 1循环进行细分迭代次数。对于行中的每一列第二个,对于每一行第三个。试图在2D中点细分中计算环路的逻辑
这就是我所拥有的。它显示了左上角方形细分的结果。这就是为什么行和列只循环一次。如果我得到基本的逻辑,其余的应该是蛋糕。但是循环在第三次迭代中不起作用。我很确定循环需要更复杂。
迭代是手动设置的变量。
// iterate though subdivision levels
for(i = 1; i <= iterations; i++) { // iteration
// iterate through each row
for(row = 1; row <= 1; row += size/i) { // row
// iterate through each column and subdivide
for(col = 1; col <= 1; col += size/i) { // column
//*** ONLY SHOWING THE FIRST SUBDIVIDE OF EACH ITERATION ***//
// Math.pow(2, iterations)/2/i = 1
// i = 1, iteration = 1
heightmap[0][1] = (heightmap[0][0] + heightmap[0][2])/2;
// Math.pow(2, iterations)/2/i = 2
// i = 1, iterations = 2
heightmap[0][2] = (heightmap[0][0] + heightmap[0][4])/2;
// Math.pow(2, iterations)/2/i = 4
// i = 1, iterations = 3
heightmap[0][4] = (heightmap[0][0] + heightmap[0][8])/2;
// if iterations == 1, i != 2
// Math.pow(2, iterations)/2/i = 1
// i = 2, iterations = 2
heightmap[0][1] = (heightmap[0][0] + heightmap[0][2])/2;
// Math.pow(2, iterations)/2/i = 2
// i = 2, iterations = 3
heightmap[0][2] = (heightmap[0][0] + heightmap[0][4])/2;
// if iterations == 1 or 2, i != 3
// Math.pow(2, iterations)/2/i = 4/3 != 1
// i = 3, iterations = 3
heightmap[0][1] = (heightmap[0][0] + heightmap[0][2])/2;
}
}
}
如果这有助于我用于一维细分。
// increase the number of segments
for(var i = 1; i < size; i *= 2){
// iterate through each segment calculating the center point
for(var j = (size/i)/2; j < size; j += size/i){
points[j] = ((points[j - (size/i)/2] + points[j + (size/i)/2])/2);
}
}