菱形方算法

我正在尝试用Java编写Diamond-Square算法来生成随机地图,但无法弄清楚实现…

任何有一些Java代码(或其他语言)的人,所以我可以检查如何制作循环将非常感谢!

谢谢!

这是一种用于生成值的有趣算法。 这是我根据维基百科文章的参考文献中的解释给出的实现。 它将创建“球形值”(包裹在所有边缘)。 注释中有注释如何更改它以在边缘上生成新值而不是包装(尽管在这些情况下边缘的平均值并不正确)。

//size of grid to generate, note this must be a //value 2^n+1 final int DATA_SIZE = 9; //an initial seed value for the corners of the data final double SEED = 1000.0; double[][] data = new double[DATA_SIZE][DATA_SIZE]; //seed the data data[0][0] = data[0][DATA_SIZE-1] = data[DATA_SIZE-1][0] = data[DATA_SIZE-1][DATA_SIZE-1] = SEED; double h = 500.0;//the range (-h -> +h) for the average offset Random r = new Random();//for the new value in range of h //side length is distance of a single square side //or distance of diagonal in diamond for(int sideLength = DATA_SIZE-1; //side length must be >= 2 so we always have //a new value (if its 1 we overwrite existing values //on the last iteration) sideLength >= 2; //each iteration we are looking at smaller squares //diamonds, and we decrease the variation of the offset sideLength /=2, h/= 2.0){ //half the length of the side of a square //or distance from diamond center to one corner //(just to make calcs below a little clearer) int halfSide = sideLength/2; //generate the new square values for(int x=0;x 

M. Jessup的答案似乎有些小问题。 他在哪里:

  double avg = 
 data [(x-halfSide + DATA_SIZE)%DATA_SIZE] [y] + //中心左侧
 data [(x + halfSide)%DATA_SIZE] [y] + //中心右侧
 data [x] [(y + halfSide)%DATA_SIZE] + //低于中心
数据[X] [(Y-halfSide + DATA_SIZE)%DATA_SIZE];  //在中心之上

它应该改为:

  double avg = 
数据[(x-halfSide + DATA_SIZE-1)%(DATA_SIZE-1)] [y] + //中心左侧
 data [(x + halfSide)%(DATA_SIZE-1)] [y] + //中心右侧
 data [x] [(y + halfSide)%(DATA_SIZE-1)] + //低于中心
数据[X] [(Y-halfSide + DATA_SIZE-1)%(DATA_SIZE-1)];  //在中心之上

否则它从错误的位置读取(可能是未初始化的)。

对于任何人来说,这里是由M. Jessup提供的算法,包含在一个接收种子的类中(允许重现结果),n的值用于指定维度(维度为2 ^ n + 1),并暴露结果作为规范化的浮点数组。 它还修复了所应用算法的第二部分。

 import java.util.Random; public class DiamondSquare { public float[][] data; public int width; public int height; public DiamondSquare(long mseed, int n) { //size of grid to generate, note this must be a //value 2^n+1 int DATA_SIZE = (1 << n) + 1; width = DATA_SIZE; height = DATA_SIZE; //an initial seed value for the corners of the data final float SEED = 1000.0f; data = new float[DATA_SIZE][DATA_SIZE]; //seed the data data[0][0] = data[0][DATA_SIZE-1] = data[DATA_SIZE-1][0] = data[DATA_SIZE-1][DATA_SIZE-1] = SEED; float valmin = Float.MAX_VALUE; float valmax = Float.MIN_VALUE; float h = 500.0f;//the range (-h -> +h) for the average offset Random r = new Random(mseed);//for the new value in range of h //side length is distance of a single square side //or distance of diagonal in diamond for(int sideLength = DATA_SIZE-1; //side length must be >= 2 so we always have //a new value (if its 1 we overwrite existing values //on the last iteration) sideLength >= 2; //each iteration we are looking at smaller squares //diamonds, and we decrease the variation of the offset sideLength /=2, h/= 2.0){ //half the length of the side of a square //or distance from diamond center to one corner //(just to make calcs below a little clearer) int halfSide = sideLength/2; //generate the new square values for(int x=0;x 

查看使用Processing完成的演示:

http://www.intelegance.net/code/diamondsquare.shtml

另外,这是另一个粗略算法的页面:

http://www.javaworld.com/javaworld/jw-08-1998/jw-08-step.html?page=2

最后,一篇更正式的论文:

http://www.student.math.uwaterloo.ca/~pmat370/PROJECTS/2006/Keith_Stanger_Fractal_Landscapes.pdf

请享用!